Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Units and Values

Number and integer values

Reference for the CSS number and integer value types: allowed syntax, unitless zero, scientific notation, and which properties take plain numbers.

CSS has two basic numeric types. An integer (<integer>) is a whole number such as 3 or -1. A number (<number>) may also have a decimal part, like 0.5, 1.25 or -.75. Neither has a unit. Properties that take them include z-index and order (integers), and opacity, flex-grow, line-height and scale() (numbers).

Lengths, angles and times are different types: they are numbers plus a unit. The one exception is 0, which is accepted as a length without a unit.

CSS
.card {
  z-index: 2;         /* integer */
  opacity: 0.8;       /* number */
  line-height: 1.5;   /* number: multiplied by the font size */
  margin: 0;          /* unitless zero is a valid length */
}

Try it

Numbers that scale things

Syntax

CSS
<integer> = [ + | - ]? <digit>+
<number>  = [ + | - ]? [ <digit>* . ]? <digit>+ [ e [ + | - ]? <digit>+ ]?

Examples of valid numbers: 0, 12, +3, -7, 0.5, .5, 1., 1e3, 2.5E-2. (A trailing dot such as 1. is not valid; write 1 or 1.0.)

Values

Type Description
<integer> Whole numbers, positive, zero or negative, with an optional sign. Used for counts, ordering and stacking.
<number> Integers or decimals, including scientific notation like 1.5e2. Used for ratios, factors and opacity.
<percentage> A number followed by %; a different type that some properties accept in place of a number (for example opacity: 50%).

Usage notes

  • Unitless zero. margin: 0 is fine. For other lengths you need a unit: margin: 5 is invalid and the declaration is dropped.
  • Unitless line-height. line-height: 1.5 is a number multiplied by each element's own font size, so it inherits as a ratio. line-height: 1.5em inherits as a fixed computed length.
  • Ranges are clamped. opacity accepts numbers below 0 or above 1, but they are clamped to the range 0 to 1.
  • No separators. Write 1000, not 1,000 or 1_000.
  • Decimal point only. Use a dot, even if your language writes decimals with a comma.
  • Integers only where required. z-index: 1.5 is invalid. order and grid-column lines also need integers.
  • calc() can mix them. calc(2 * 3) is a number; calc(2 * 1rem) is a length.

Browser support

Number and integer syntax is supported by every browser.