Units in CSS

Syntax for CSS units.

Most values require the unit to be specified. Failure to do so most of the time makes the rule invalid. It is only when the value is 0 that we can dispense with specifying the unit.

The unit is inscribed after the numerical value, without spaces:
Examples : 12px, 45deg, etc.

Table of Contents.

Absolute dimension units.

These units can be used for anything that is a dimension: width and height of a block, margins, font size, etc.

They are absolute units in that the value is not calculated relative to any other dimension.

px
Pixel. A pixel is about 1/96th of an inch, but this can vary from one screen to another.
On mobiles and some high-definition screens, a pixel can correspond to several physical points on the screen.
pt
Point. One dot = 1/72nd of an inch.
pc
Pica. One pica is worth 1/6th of an inch, or 12 points.
The pica is a unit originating in the printing press. It is rarely used nowadays.
in
Inch. An inch is 2.54 cm.
in is the abbreviation for inch.
cm
mm
Centimetre et Millimetre are part of the International System of Units (SI). Both of these units are widely used in the whole world. However, they are not very practical for defining dimensions on a screen: they should be reserved for the dimensions of the printed pages.
Q
A Q is 1/40 cm. This unit is little used. It is not recognized by all sailors.

Relative Dimension Units.

These units cause a computation from another dimension. For example, a margin of 25% measures a quarter of the dimension of the parent element.

Relative units are increasingly being used to create layouts that adapt to more and more diverse screen sizes (from the screen from a mobile phone to the TV screen). The term "responsive" refers to a layout that is perfectly adaptable to the size of the screen.

%
Percentages are calculated against another dimension, either of the element itself, or of the parent element, and so on. Depending on the property, this other dimension can be the width, the line height, etc.
fr
(fraction). This unit is reserved for sizing column widths or row heights in a grid. It is a unit relative to the remaining space.

Elements sized in fr distribute the remaining space in the grid container according to their fr value.

In the example below, observe that the last two columns occupy all the remaining space to the right of the first column, and that column 3 is always twice as wide as column 2.

width = 100px
width = 1fr
width = 2fr

 

Viewport dimension units.

vw (Viewport Width)= 1/100 of the width of the viewport.
vh (Viewport Height) = 1/100 of the height of the viewport.
vi (Viewport Inline) = 1/100 of the Viewport dimension in line direction (inline axis) of the root element.
vb (Viewport Block) = 1/100 of the dimension of the viewport in the direction of the block axis of the root element.

The viewport is the available part of the screen to display the HTML document or page. So it excludes scroll bars, toolbars of the browser, optional windows like the console, etc.

Notice that the dimensions of the blue block below are well proportional to the dimensions of the screen by varying the width and height of the The browser window (if you're viewing this page on a desktop computer).

width:25vw;
height:10vh;
vmin (Viewport min) = 1/100 of the smallest of the viewport dimensions.
vmax (Viewport max) = 1/100 of the largest of the viewport dimensions.

In the case of a horizontal screen such as a desktop computer screen, vmin will therefore correspond to the height of the viewport, but for a portable phone vmin will be the width.

New Viewport Units.

lvw, lvh, lvi, lvb, lvmax, lvmin,
svw, svh, svi, svb, svmax, svmin,
dvw, dvh, dvi, dvb, dvmax, dvmin,

Why did the W3C introduce these nuances? Because browsers can show or hide certain elements to give the page as much space as possible. This is especially the case on mobiles: the address bar, scroll bars, etc. are only displayed when they are useful.

This dynamic display management can impact the size of the elements defined in relation to the viewport, which requires recalculation each time the layout. These units were created to prevent this.

Units that start with L (Large) refer to the largest possible ViewPort dimension, when all optional elements are masked. Items whose dimensions are set with these units can be slightly hidden when an accessory is displayed.

Conversely, those that start with s (small) refer to the smallest dimension of the viewport, when all optional elements are displayed. Elements whose dimensions are defined with these units will never be hidden regardless of the accessories that the browser can bring to light, but when these elements disappear, the occupancy of the available space is less good.

Finally, those that start with d (dynamic) refer to the current dimension of the viewport. The dimensions defined with these units therefore evolve according to the elements that are displayed or hidden. These units have none of the disadvantages mentioned above, but require the layout to be recalculated each time an element is displayed or masks.

The elements below have their height defined in each of the units. The difference is not significant, and will probably be invisible on a screen computer because few visual elements are optional (the address bar is always displayed for example). But on a mobile, the block defined In swh must be slightly lower than the others.
By bringing up the address bar (by scrolling the screen slightly), only the block defined in dvh should change height.

lvh
svh
dvh
vh

Non-specific units like vw, vh, vi, and vb are equivalent to respectively lvw, lvh, lvi and lvb.

Dimension units relating to the size of the characters.

em
rem
The unit of character size of the parent element.
Unity relative to the size of the root element.

Setting all character sizes to em or rem is a good solution to dynamically change the font size (magnifying glass function) because it is then enough to change the size of the characters of the root element so that the size of all the characters on the page is set Up to date.
The root element can be designated either by the html selector or by the :root pseudo-class.

CSS unit em = character height
ex
rex
The unit relative to the height of the parent element's lowercase x.
Unit relative to the height of the root element's lowercase x characters.

CSS unit ex = lowercase x letter height

cap
rcap
Unit relative to the height of the capital letters of the element itself.
Unity relative to the height of the capital letters of the root element.
CSS unit cap = capital height
lh
rlh
A unit relative to the height of a line of text in the element itself.
Unit relative to the height of a line of text in the root element.

CSS unit lh = row height

ch
rch
Unit relative to the width of the 0 of the element itself.
Unit relative to the width of the 0 of the root element.

Units relative to the width of the digit zero (0).
This unit can be used, for example, to define the width of an input area. Viewing the CSS code, you'll notice that we've added 8 pixels, which correspond to the thickness of the borders and padding (padding).

Postal code:

 

ic
ric
The unit relative to the width of the 水 character in the font of the element itself.
The unit relative to the width of the 水 character in the root element font.

This unit is of course used mainly for texts in the language CJK.

Dimension units that can be used in container queries.

Container queries allow you to define conditional styles based on the dimensions of a container element. Refer to the @container page for a detailed presentation of container queries.

cqw
1% of the width of the container.
cqh
1% of the height of the container.
cqi
1% of the container dimension in the direction of the lines.
cqb
1% of the container dimension in the direction of the blocks.
cqmin
1% of the smaller container dimension between cqi and cqb.
cqmax
1% of the largest container width between cqi and cqb.

Angle units.

Corner units are mostly used for transformations, but a few other properties accept angular values. For example, font-style to set character skewing, linear gradient functions, color hue rotation functions, etc.

deg
Degree: Classic angular unit: 90 degrees is a right angle, 360 degrees is a full turn.
grad
Grade: Other Angular Unit: 100 degrees is a right angle, 400 degrees is a full turn.
rad
Radian: this angular unit is widely used in mathematics. It is defined in relation to the number pi (3.1416...)
pi / 2 (i.e. 1.57rad) is a right angle.
2 pi (i.e. 6.28 rad) corresponds to a complete turn.
turn
Turn: 1turn is a full turn. So 0.25turn is a 90-degree angle.

Resolution units.

These units of resolution are used by media queries. Refer to the @media directive for more details.

dpi
Dot Per Inch : unit of resolution. 1dpi is one dot per inch.
dpcm
Dot Per cm : dots per centimetre.
dppx
Dot Per pixel) : dots per pixel. 1dppx = 96dpi.

Duration Units.

These units are mostly used to set the duration of animations or transitions.

s
Second.
ms
Millisecond.

Frequency Units.

These units are used primarily for sound reproduction.

hz
Hertz. The number of hertz corresponds to the number of vibrations per second. If it is a sound, it will be all the higher the number of hertz. The human voice is between 200hz and 450hz.
khz
Kilo-Hertz : 1khz is of course equal to 1000 hertz.
st
Semi-Tone. 1st is equal to a semitone, referring to the classical musical scale: 1st corresponds to the frequency difference between e.g. DO and C#.

Sound volume unit.

Unit used for sound reproduction.

dB
DeciBel. The decibel is a logarithmic unit used to measure the ratio between two sound intensities. If s1 and s2 are two sound intensities, their decibel ratio is calculated as follows:
Ratio = 10 x Log10 (S1 / S2)
This unit is misleading because it is logarithmic. For example, a doubling of the sound intensity corresponds only to 3dB.