Zero Values and Unitless Numbers
You'll learn when you can safely skip units in CSS and why it matters.
When Units Disappear
In CSS, numbers usually need units (10px, 2rem, 50%). But there are special cases where units become optional—or even forbidden.
Zero Values: Free to Go Unitless
When a value is zero, the unit doesn't matter. Zero pixels equals zero rems equals zero inches—they're all nothing! CSS lets you write just 0 instead of 0px or 0em:
.box {
margin: 0; /* Valid - no unit needed */
padding: 0 20px; /* First value unitless, second needs unit */
border-width: 0; /* Valid */
}
This saves characters and makes code cleaner. margin: 0px works too, but experienced developers prefer the shorter 0.
Unitless Properties
Some CSS properties expect unitless numbers because they represent ratios or multipliers, not physical measurements:
line-height — The most common example. When unitless, it multiplies the element's font size:
p {
font-size: 16px;
line-height: 1.5; /* Computes to 24px (16 × 1.5) */
}
opacity — Values from 0 to 1 (no unit):
.faded {
opacity: 0.5; /* 50% transparent */
}
z-index — Stacking order (integers only):
.modal {
z-index: 100; /* No unit */
}
Why It Matters
Writing 0 instead of 0px is a small efficiency win. Understanding unitless properties prevents errors—writing line-height: 1.5px would create a tiny, unusable line height instead of the intended proportional spacing.
Key Takeaway: Zero values can omit units for brevity; certain properties like line-height, opacity, and z-index require unitless numbers because they represent ratios or abstract scales, not measurements.