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.
.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
Syntax
<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: 0is fine. For other lengths you need a unit:margin: 5is invalid and the declaration is dropped. - Unitless
line-height.line-height: 1.5is a number multiplied by each element's own font size, so it inherits as a ratio.line-height: 1.5eminherits as a fixed computed length. - Ranges are clamped.
opacityaccepts numbers below 0 or above 1, but they are clamped to the range 0 to 1. - No separators. Write
1000, not1,000or1_000. - Decimal point only. Use a dot, even if your language writes decimals with a comma.
- Integers only where required.
z-index: 1.5is invalid.orderandgrid-columnlines 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.
Related
opacityโ number or percentagez-indexโ integerline-heightโ unitless numberscalc()โ arithmetic with numbers- Lesson: Units: px, em, rem and More