</>
ValidateHTML

Missing Unit on CSS Value

In CSS, numeric values (except 0) must have a unit. Writing 'margin: 10' instead of 'margin: 10px' is invalid. The only exceptions are unitless values like line-height, opacity, flex-grow, z-index, and font-weight, which accept plain numbers by design.

Why It Matters

A missing unit causes the entire declaration to be ignored. The property reverts to its default or inherited value, which can cause layout shifts, spacing issues, or invisible elements.

Common Causes

  • Copying a raw number from a design tool that shows sizes without units.
  • Assuming a length property accepts a bare number the way line-height or opacity does.
  • Dropping the unit on one value inside a shorthand, such as padding: 10px 15.

Code Examples

Bad CSS
div {
  margin: 20;          /* needs a unit */
  padding: 10 15;      /* both need units */
  font-size: 16;       /* needs a unit */
  border: 1 solid red; /* width needs unit */
}
Good CSS
div {
  margin: 20px;
  padding: 10px 15px;
  font-size: 16px;
  border: 1px solid red;
}

How to Fix

  • 1Always add a unit to non-zero numeric values: px, em, rem, %, vh, vw.
  • 2Exception: 0 never needs a unit. 'margin: 0' is correct (not 'margin: 0px').
  • 3Properties that accept unitless numbers: line-height, opacity, z-index, flex-grow, flex-shrink, font-weight, order.
  • 4When in doubt, use px for fixed sizes, rem for scalable sizes, % for relative sizes.

Frequently Asked Questions

Which CSS properties accept a number with no unit?
Unitless numbers are valid for line-height, opacity, z-index, flex-grow, flex-shrink, order, and font-weight. Length properties like margin, padding, width, and font-size always require a unit on non-zero values.
Does zero need a unit?
No. Zero is the one number that is valid with or without a unit, because zero length is the same in every unit. Writing margin: 0 is correct and slightly shorter than margin: 0px.
Why is a unitless line-height fine but a unitless margin is not?
line-height is defined to accept a plain number as a multiplier of the font size, which scales correctly with inheritance. margin is a length, and lengths have no meaning without a unit, so a bare number is rejected.

Check Your CSS Now

Our CSS validator detects this error automatically and shows the exact line number.

Open CSS Validator

Related CSS Errors

View all CSS errors