Text & Content Lesson 12 of 33
CSS Units — px, %, em, rem, vw and vh Explained
Understand absolute and relative CSS units — pixels, percentages, em, rem, viewport units — and learn which one to use for fonts, spacing and layout.
Almost every size in CSS needs a unit: 16px, 2rem, 50%. Choosing the right unit is what makes a design look good on both a small phone and a big laptop. This lesson explains the units you'll actually use.
Units come in two kinds:
- Absolute — a fixed size that doesn't change, like
px - Relative — a size based on something else, like the parent's width, the font size, or the screen size
px — pixels
A CSS pixel is a small fixed unit. 16px is always 16px, no matter where it is. Pixels are easy to understand and good for things that shouldn't scale, like a thin 1px border.
Fun fact: a CSS pixel isn't exactly one dot on your screen. Modern phones have 2 or 3 tiny screen dots per CSS pixel, which is why text looks so sharp on them.
% — percentage
A percentage is relative to the parent element. width: 50% means half the parent's width.
<style>
.parent { width: 300px; background: #e3f1ec; padding: 6px; margin-bottom: 10px; }
.wide { width: 100%; }
.child { width: 50%; background: #0f5c4d; color: white; padding: 6px; box-sizing: border-box; }
</style>
<div class="parent"><div class="child">50% of 300px</div></div>
<div class="parent wide"><div class="child">50% of the full width</div></div>
em — relative to the font size
1em equals the current element's font size (for font-size itself, the parent's font size). If a button's font size is 20px, then padding: 0.5em is 10px. That's useful because the padding grows when the text grows:
<style>
.btn { padding: 0.5em 1.2em; background: #0f5c4d; color: white; border: 0; border-radius: 0.4em; margin: 4px; }
</style>
<button class="btn" style="font-size: 14px;">Small</button>
<button class="btn" style="font-size: 18px;">Medium</button>
<button class="btn" style="font-size: 26px;">Large</button>
One rule creates three nicely proportioned buttons. Change one font size and its padding and corners adjust too.
rem — relative to the root font size
rem means "root em": it's relative to the font size of the <html> element, which is 16px by default. So 1rem = 16px, 1.5rem = 24px, 2rem = 32px, no matter how deeply nested the element is.
The big advantage: if a visitor sets a bigger default text size in their browser or phone settings (common for people with weak eyesight, or for older people), everything in rem grows with it. Pixel font sizes don't.
h1 { font-size: 2rem; } /* 32px normally */
p { font-size: 1rem; } /* 16px normally */
.card { padding: 1.5rem; } /* 24px normally */
vw and vh — viewport units
The viewport is the visible area of the browser window. 1vw is 1% of the viewport's width, and 1vh is 1% of its height.
height: 100vh— exactly as tall as the screen (great for a full-screen welcome section)width: 50vw— half the screen width
<style>
body { margin: 0; }
.welcome {
height: 100vh;
display: grid;
place-items: center;
background: #0f5c4d;
color: white;
font: bold 6vw Arial, sans-serif;
}
</style>
<section class="welcome">Welcome, Batch 2026!</section>
Here the section fills the frame's full height, and the text size is 6% of the width, so it grows and shrinks as the frame is resized.
Other units you might see
ch— the width of the "0" character.max-width: 65chkeeps lines about 65 characters long, a comfortable reading width.fr— a "fraction" of free space, used only in CSS Grid. You'll meet it in the Grid lesson.cm,mm,in,pt— print units. Use them only in style sheets made for printing.
Zero needs no unit
margin: 0 is the same as margin: 0px. Zero is zero in any unit, so the unit is optional. Every other number needs one: margin: 10 (without px) is invalid and is ignored.
Which unit should I use?
| For… | Good choice |
|---|---|
| Font sizes | rem |
| Padding on buttons and small parts | em or rem |
| Widths of columns and boxes | %, fr, or max-width in rem/px |
| Full-screen sections | vh (with min-height) |
| Borders and thin lines | px |
| Readable line length | ch |
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04