On this page9 sections
Lecture 007: Width, Height, Units & Overflow
| Category | 2: The Box Model |
| Lecture | 007 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
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-widthandmin-height - Choose between
px,%,em,rem,vwandvh - Handle content that is too big with
overflow
1. Width and Height
Example
<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:
<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
| Unit | Relative to | Good for |
|---|---|---|
px | Nothing: a fixed size | Borders, small details |
% | The parent element's size | Widths in layouts |
rem | The root (page) font size, usually 16px | Font sizes and spacing |
em | The current element's font size | Padding that grows with the text |
vw | 1% of the browser window's width | Full-width sections |
vh | 1% of the window's height | Full-screen hero sections |
Example
<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
<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>| Value | Effect |
|---|---|
visible | Default. Content spills out of the box |
hidden | Extra content is cut off |
auto | A 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
- Which property lets a box shrink on phones but never grow wider than 600px?
- The root font size is 16px. How big is
2rem? - What does
100vhmean? - A child has
width: 25%inside a 400px parent. How wide is the child? - Which
overflowvalue shows a scrollbar only when needed? - Why is
remusually better thanemfor font sizes?
Practice Answers
max-width: 600px;- 32px.
- The full height of the browser window.
- 100px.
overflow: auto;remalways refers to the root size, so it doesn't compound when elements are nested.
Key Takeaways
- Set widths, not heights;
max-widthis the most useful sizing rule. px= fixed,%= parent,rem= root font,em= own font,vw/vh= window.overflowdecides what happens to content that doesn't fit.- Flexible units make one layout work on every screen size.