On this page9 sections
  1. Learning Objectives
  2. 1. Width and Height
  3. 2. max-width: Wide, But Not Too Wide
  4. 3. The Units
  5. 4. When Content Doesn't Fit: overflow
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 007: Width, Height, Units & Overflow

Category2: The Box Model
Lecture007 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelBeginner

How wide is "wide"? CSS can measure in fixed pixels, as a percentage of the parent, as a share of the screen, or relative to the text size. Picking the right unit is what makes a layout work on phones and laptops alike.


Learning Objectives

  • Set width, height, max-width and min-height
  • Choose between px, %, em, rem, vw and vh
  • Handle content that is too big with overflow

1. Width and Height

Example

html
<style>
  .box { width: 220px; height: 90px; background: #ffd8a8; padding: 8px; }
</style>

<div class="box">220 × 90 pixels</div>

Block elements are full-width by default, and their height grows to fit their content. Usually you set widths and leave heights alone.


2. max-width: Wide, But Not Too Wide

Lines of text that are very long are tiring to read. max-width lets an element shrink on small screens but stops it growing past a limit.

Example: resize the preview, or picture it on a phone:

html
<style>
  .article { max-width: 480px; margin: 0 auto; background: #f1f3f5; padding: 12px; }
</style>

<div class="article">
  I am never wider than 480px, but on a narrow phone screen I shrink to fit.
  This is the most useful width rule for page content.
</div>

min-height works the other way: "at least this tall, taller if the content needs it".


3. The Units

UnitRelative toGood for
pxNothing: a fixed sizeBorders, small details
%The parent element's sizeWidths in layouts
remThe root (page) font size, usually 16pxFont sizes and spacing
emThe current element's font sizePadding that grows with the text
vw1% of the browser window's widthFull-width sections
vh1% of the window's heightFull-screen hero sections

Example

html
<style>
  .parent { width: 300px; background: #e9ecef; padding: 4px; }
  .half   { width: 50%; background: #74c0fc; }
  .big    { font-size: 1.5rem; }      /* 1.5 × 16px = 24px */
  .btn    { font-size: 20px; padding: 0.5em 1em; background: #51cf66; display: inline-block; }
</style>

<div class="parent"><div class="half">50% of a 300px parent</div></div>
<p class="big">1.5rem text</p>
<span class="btn">em padding grows with the text</span>

A good default: rem for font sizes, % or max-width for layout widths, px for borders.


4. When Content Doesn't Fit: overflow

Example

html
<style>
  .box { width: 220px; height: 70px; border: 2px solid #868e96; margin-bottom: 10px; }
  .hidden { overflow: hidden; }
  .scroll { overflow: auto; }
</style>

<div class="box hidden">overflow: hidden cuts off the extra text, so anything that does not fit is simply not shown.</div>
<div class="box scroll">overflow: auto adds a scrollbar only when the text does not fit inside the box.</div>
ValueEffect
visibleDefault. Content spills out of the box
hiddenExtra content is cut off
autoA scrollbar appears only when needed

Common Beginner Mistakes

1. Fixed widths everywhere. width: 1000px overflows a 390px phone. Prefer max-width.

2. Fixed heights on text boxes. When the text grows, it spills out. Let height be automatic, or use min-height.

3. % heights that do nothing. A percentage height needs a parent with a set height.

4. Nested em surprises. em multiplies at every level, so 1.2em inside 1.2em is 1.44 times the size. rem avoids this.


Practice Questions

  1. Which property lets a box shrink on phones but never grow wider than 600px?
  2. The root font size is 16px. How big is 2rem?
  3. What does 100vh mean?
  4. A child has width: 25% inside a 400px parent. How wide is the child?
  5. Which overflow value shows a scrollbar only when needed?
  6. Why is rem usually better than em for font sizes?

Practice Answers

  1. max-width: 600px;
  2. 32px.
  3. The full height of the browser window.
  4. 100px.
  5. overflow: auto;
  6. rem always refers to the root size, so it doesn't compound when elements are nested.

Key Takeaways

  • Set widths, not heights; max-width is the most useful sizing rule.
  • px = fixed, % = parent, rem = root font, em = own font, vw/vh = window.
  • overflow decides what happens to content that doesn't fit.
  • Flexible units make one layout work on every screen size.