CSS units and sizing: px, %, rem, vw and clamp()
Size things so they adapt: pixels, percentages, rem and em, viewport units, max-width and fluid clamp().
In this lesson you will
- Choose the right unit for each job
- Combine width and max-width
- Avoid fixed heights
- Make fluid type with clamp()
About 14 minutes
How big is big?
You are designing a card. You write width: 400px and it looks fine on your laptop. On a phone that is 360 pixels wide, the card pokes out the side of the screen. On a large monitor it looks tiny. The problem is not the number, it is the unit. A pixel is a fixed amount, while screens come in all sizes. Choosing the right unit is how a page adapts instead of breaking.
Absolute and relative units
Absolute units stay the same however the screen changes. Relative units are calculated from something else, so they flex.
Pixels
px is the one you have been using. It is predictable and great for small details such as borders (1px), shadows and small gaps. It is a poor choice for font sizes and for the width of main layout blocks, since it ignores the visitor’s settings and the screen size.
Percentages
.column { width: 50%; }
A percentage is a share of the parent element’s size. If the parent is 800 pixels wide, 50% is 400. If the parent shrinks to 300, it becomes 150. This makes percentage widths ideal for flexible layouts.
rem and em
remis a multiple of the root font size, 16 pixels by default.2remis 32 pixels. Use it for font sizes and spacing so everything scales together.emis a multiple of the current element’s font size. It is useful for spacing inside a component that should grow with its text, such as button padding, but it compounds when nested, so be careful.
body { font-size: 1rem; }
h1 { font-size: 2.5rem; margin-bottom: 1rem; }
.btn { font-size: 1rem; padding: .6em 1.2em; }
Viewport units
.hero { min-height: 60vh; } /* 60% of the screen height */
.title { font-size: 5vw; } /* 5% of the screen width */
vw and vh are 1 percent of the viewport width and height. They are perfect for full-screen sections. For mobile browsers, whose address bars change the visible height, dvh (dynamic viewport height) is often more accurate than vh.
Replace the fixed pixel width with a flexible one so the box fills 90% of its parent but never grows beyond 500px. Use width and max-width.
<style>
.box { width: 700px; background: #EFEAFD; padding: 16px; border-radius: 12px; font-family: Arial; box-sizing: border-box; }
</style>
<div class="box">Resize the preview frame and watch what happens to me.</div>
Set width: 90%; max-width: 500px; and keep the padding.
<style>
.box { width: 90%; max-width: 500px; background: #EFEAFD; padding: 16px; border-radius: 12px; font-family: Arial; box-sizing: border-box; }
</style>
<div class="box">Resize the preview frame and watch what happens to me.</div>
Width, max-width and min-width
These three properties work as a team. width is the preferred size. max-width sets a ceiling it may not exceed. min-width sets a floor. The winning combination for most content is a flexible width with a maximum.
.container {
width: 100%;
max-width: 1100px;
margin: 0 auto;
padding: 0 20px;
}
On a small screen it fills the width. On a large screen it stops growing at 1100 pixels and sits centred. You will use this pattern on almost every site you build.
Height: let content decide
A fixed height is risky because text can overflow when it is longer than you planned, or when a visitor increases the font size. In most cases, leave height alone and let the content decide. Use min-height if you need a minimum, such as for a hero section.
Setting height: 100px on a box that contains text. When the text wraps onto more lines on a phone, it spills out of the box and overlaps the content beneath.
Overflow
.log { height: 200px; overflow: auto; }
When content is larger than its box, overflow decides what happens: visible (the default, it spills out), hidden (clipped), scroll (always scrollbars) or auto (scrollbars only when needed).
Modern helpers: clamp, min and max
h1 { font-size: clamp(1.75rem, 4vw, 3rem); }
.wrap { width: min(100% - 40px, 1100px); }
clamp(min, preferred, max) gives a value that grows with the screen but never goes below the minimum or above the maximum. The heading above is 28 pixels on a tiny phone, 48 pixels on a large screen, and smoothly scales in between. No media queries needed.
rem for fonts and spacing, % and max-width for layout widths, px for borders and shadows, vh for full-screen sections, and clamp() for fluid headings.
Make the heading fluid: use clamp so it is at least 1.5rem, at most 3rem and 5vw in between.
<style>
h1 { font-size: 24px; font-family: Arial; }
</style>
<h1>A fluid heading</h1>
font-size: clamp(1.5rem, 5vw, 3rem);
<style>
h1 { font-size: clamp(1.5rem, 5vw, 3rem); font-family: Arial; }
</style>
<h1>A fluid heading</h1>
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
A percentage width is a share of what?
- The screen
- The parent element
- The font size
- The document
Percentages refer to the parent’s size.
What is 1rem by default?
- 10px
- 16px
- 12px
- The parent’s font size
rem is the root font size, 16px by default.
What does max-width: 1100px do?
- Forces width 1100px
- Stops the element growing beyond 1100px
- Hides it above 1100px
- Sets the minimum
It sets a ceiling on the width.
Why avoid a fixed height on text boxes?
- Browsers reject it
- Text can overflow when it wraps onto more lines
- It looks ugly
- It is slower
Content size varies. Let it set the height.
What does clamp(1rem, 3vw, 2rem) do?
- Always 3vw
- Fluid size between 1rem and 2rem
- A fixed 2rem
- Errors
It scales with the screen but stays within the minimum and maximum.
Summary
- Use rem for text, % and max-width for layouts, px for details.
- Let content decide height.
- clamp() gives fluid sizes without media queries.
- Viewport units suit full-screen sections.
Questions
px or rem for font sizes?
rem, because it respects the visitor's text size settings.
What is the difference between em and rem?
em is relative to the element's own font size and compounds when nested. rem is always relative to the root.
Why does 100vh cause scrolling on phones?
Mobile address bars take space. Use dvh or min-height for a more accurate full-screen height.
