CSS Units
In this page:
selector {
width: number + unit; /* px, em, rem, %, vw, vh */
}
Rigid Pixel Units (px)
Pixels (px) are absolute units. A pixel represents a single physical dot of color on a standard computer screen. Because pixels are absolute, they are completely rigid and do not scale relative to other elements or browser settings.
- Use pixel units for properties that require exact, unyielding precision, such as border widths.
- Accepted values:
- 1px — one CSS pixel (about 1/96 inch)
- <number>px — any integer or decimal length
- 0 — unitless zero is allowed for lengths
Example: Rigid Pixel Units (px)
<style>
.box {
border: 2px solid black;
}
</style>
<div class="box">2px border stays exactly 2px always</div>
Flexible Root em Units (rem)
The rem (Root Em) unit is a relative unit. It calculates values relative to the base font size of the root html element (which is 16px by default in most browsers). This makes rem units highly accessible and responsive.
- Use rem units for all typography, padding, and margins to ensure they scale proportionally with browser text settings.
- Accepted values:
- 1rem — equals the root html font-size (16px by default)
- 1.5rem — 1.5 times the root font-size
- 0.5rem — half the root font-size
Example: Flexible Root em Units (rem)
<style>
html { font-size: 16px; }
p { font-size: 1.5rem; }
</style>
<p>1.5rem = 1.5 times the root font size</p>
Contextual em Units (em)
The em unit is a relative unit that calculates values relative to the font size of the element's parent container. This makes em units highly useful for scaling spacing (like padding) proportionally with the text size of that specific component.
- Use em units for padding and margins inside buttons to keep spacing proportional to the text size.
- Accepted values:
- 1em — equals the element's own font-size (or parent's, for font-size itself)
- 1.5em — 1.5 times that font-size
- 0.5em — half of that font-size
Example: Contextual em Units (em)
<style>
.parent { font-size: 20px; }
.child { padding: 1em; }
</style>
<div class="parent">
<span class="child">1em padding = 20px, matching the parent's font-size</span>
</div>
Viewport Width and Height (vw/vh)
Viewport units are relative to the browser window size: 1vw equals 1% of the viewport width, and 1vh equals 1% of the viewport height. This makes them perfect for building full-screen layouts.
- Use height: 100vh to quickly create full-screen landing pages or hero section grids.
- Accepted values:
- 1vw — 1% of the viewport width
- 1vh — 1% of the viewport height
- 100vw / 100vh — full viewport width or height
- 1dvh — 1% of the dynamic viewport height (mobile-friendly)
Example: Viewport Width and Height (vw/vh)
<style>
.hero {
height: 100vh;
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
display: flex;
align-items: center;
justify-content: center;
}
</style>
<div class="hero">Always fills the full viewport height</div>
Percentage Units (%)
The percentage (%) unit is a relative unit that calculates values relative to the size of the parent container element, making it perfect for building fluid multi-column grids.
- Set box-sizing: border-box globally to prevent padding from breaking percentage column grids.
- Accepted values:
- 50% — half of the reference size (usually the parent)
- 100% — full parent size
- <number>% — any percentage the property allows
Example: Percentage Units (%)
<style>
.parent {
width: 500px;
background: #ddd;
padding: 8px;
}
.child {
width: 50%;
background: coral;
color: white;
padding: 8px;
}
</style>
<div class="parent">
<div class="child">50% of its 500px parent</div>
</div>
- Using absolute pixel units for font sizes, which prevents users from scaling text in browser settings and violates web accessibility guidelines.
- Nesting multiple em-sized elements recursively, causing font sizes to multiply exponentially and grow out of control.
- Overusing viewport height units (vh) on mobile screens, which can cause content to overlap with native system status bars.
- Use rigid pixels (px) only for properties requiring absolute, unyielding precision (like border widths).
- Use rem units for accessible typography, and em units to scale component padding proportionally with text size.
- Build fluid columns using percentage units (%), and full-screen layouts using viewport width (vw) and height (vh) units.
Standard CSS units (px, rem, em, %, vw, vh) are supported natively by all modern web browsers.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: