CSS Values & Units

CSS values determine how properties are applied to elements. Values can be keywords, numbers, percentages, dimensions, functions, colors, URLs, and other CSS data types.

Many numeric CSS values use units that describe lengths, angles, time, frequency, resolution, or other quantities. Some units are absolute, while others are relative to fonts, the viewport, a query container, or another reference value.

CSS Values

A CSS declaration assigns a value to a property. The value determines how that property affects the selected element.

p {
  font-size: 1.25rem;
  line-height: 1.6;
  color: #333;
}

In this example, 1.25rem, 1.6, and #333 are CSS values. Different properties accept different types of values.

For definitions of value types such as <length>, <percentage>, and <color>, see the CSS Data Type List.

CSS-wide Keywords

CSS-wide keywords can be used with CSS properties to control inheritance, initial values, and the effects of the cascade.

Keyword Description
inherit Uses the computed value of the property from the element's parent.
initial Uses the property's initial value.
revert Rolls the property back to the value established by an earlier cascade origin.
revert-layer Rolls the property back to the value established in an earlier cascade layer.
unset Behaves like inherit for inherited properties and initial for non-inherited properties.

Numbers & Percentages

CSS uses numbers in many forms. An <integer> is a whole number, while a <number> can contain a fractional component. Percentages use the % symbol and are calculated relative to a value defined by the property.

.example {
  opacity: 0.8;
  z-index: 10;
  width: 75%;
}

Numbers do not always require a unit. Whether a unit is allowed or required depends on the property and the type of value it accepts.

Length Units

CSS length values represent distances. A length consists of a number followed immediately by a length unit, such as 20px, 2rem, or 50vw.

Length units are generally divided into absolute and relative units. Absolute units have fixed relationships to one another, while relative units are calculated from another measurement such as a font size, viewport dimension, or query container. A zero length can usually be written without a unit, such as margin: 0;.

Absolute Length Units

Absolute length units have fixed relationships to one another. CSS defines 1in as 96px, with the other absolute units defined relative to those measurements.

Unit Name Relationship
cm Centimeters 1cm = 96px / 2.54
mm Millimeters 1mm = 1cm / 10
Q Quarter-millimeters 1Q = 1cm / 40
in Inches 1in = 96px
pc Picas 1pc = 1in / 6
pt Points 1pt = 1in / 72
px Pixels 1px = 1in / 96

A CSS pixel is a reference unit and does not necessarily correspond to one physical device pixel.

Font-relative Length Units

Font-relative units calculate lengths from font metrics. They are useful for typography and layouts that should scale with text.

Unit Relative To
em The element's font size, with special behavior when used by font-size.
ex The x-height of the element's font.
cap The cap height of the element's font.
ch The advance measure of the 0 glyph in the element's font.
ic The advance measure of the full-width ideographic glyph used for this unit.
lh The element's line height.
rem The root element's font size.
rex The x-height of the root element's font.
rcap The cap height of the root element's font.
rch The advance measure of the 0 glyph in the root element's font.
ric The advance measure of the full-width ideographic glyph for the root element.
rlh The root element's line height.

Viewport Units

Viewport units are relative to dimensions of the viewport. CSS provides default viewport units along with small, large, and dynamic viewport variants that account for changes in browser interface areas. Default viewport units generally behave like the large viewport variants.

Group Units
Default viewport vw, vh, vi, vb, vmin, vmax
Small viewport svw, svh, svi, svb, svmin, svmax
Large viewport lvw, lvh, lvi, lvb, lvmin, lvmax
Dynamic viewport dvw, dvh, dvi, dvb, dvmin, dvmax

For example, 1vw represents 1% of the viewport width, while 1vh represents 1% of the viewport height. On mobile devices, dvh can be useful when the visible viewport changes as browser controls appear or disappear.

Container Query Units

Container query length units are relative to the dimensions of an eligible query container rather than the viewport. They are useful when a component should scale according to the space available in its container. If no eligible container exists for an axis, the unit uses the small viewport size for that axis.

Unit Relative To
cqw 1% of the query container's width.
cqh 1% of the query container's height.
cqi 1% of the query container's inline size.
cqb 1% of the query container's block size.
cqmin The smaller of cqi and cqb.
cqmax The larger of cqi and cqb.

Angle Units

Angle units represent rotations and other angular values.

Unit Name Full Circle
deg Degrees 360deg
grad Gradians 400grad
rad Radians 2π radians (approximately 6.283185rad)
turn Turns 1turn

Time Units

Time values are commonly used for animations and transitions.

Unit Name Relationship
s Seconds 1s = 1000ms
ms Milliseconds 1000ms = 1s

Frequency Units

Frequency values represent the number of occurrences per unit of time.

Unit Name Relationship
Hz Hertz One cycle per second.
kHz Kilohertz 1kHz = 1000Hz

Resolution Units

Resolution units describe pixel density and are useful in contexts such as resolution media queries and image selection.

Unit Description
dpi Dots per CSS inch.
dpcm Dots per CSS centimeter.
dppx Dots per CSS pixel.
x An alias for dppx.

Flex Unit

The fr unit represents a fraction of the leftover space available to flexible tracks in a CSS Grid container. Unlike the general units defined by CSS Values and Units, fr is defined by CSS Grid Layout.

.grid {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
}

In this example, the flexible tracks share the available space in a 1:2:1 ratio. Track content and minimum sizing requirements can affect the final widths.

CSS Unit Summary

Category Units
Absolute length cm, mm, Q, in, pc, pt, px
Font-relative length em, ex, cap, ch, ic, lh, rem, rex, rcap, rch, ric, rlh
Viewport length vw, vh, vi, vb, vmin, vmax and their sv*, lv*, and dv* variants
Container query length cqw, cqh, cqi, cqb, cqmin, cqmax
Angle deg, grad, rad, turn
Time s, ms
Frequency Hz, kHz
Resolution dpi, dpcm, dppx, x
Flex fr