CSS Length Units

There are two kinds of units that can be applied to lengths in CSS. They are

  • Absolute
  • Relative

Absolute Length Units

UnitDescriptionExample
cmcentimetersfont-size: 1cm;
mmmillimetersfont-size: 5mm;
ininches (1in = 96px = 2.54cm)font-size: 0.4in;
px *pixels (1px = 1/96th of 1in)font-size: 12px;
ptpoints (1pt = 1/72 of 1in)font-size: 5pt;
pcpicas (1pc = 12 pt)font-size: 1pt;
ADVERTISEMENT

Relative Length Units

UnitDescriptionExample
emRelative to the font-size of the element (2em means 2 times the size of the current font)font-size: 2em;
exRelative to the x-height of the current font (rarely used)font-size: 3ex;
chRelative to the width of the “0” (zero)font-size: 3ch;
remRelative to font-size of the root elementfont-size: 2.5rem;
vwRelative to 1% of the width of the viewport*font-size: 3vw;
vhRelative to 1% of the height of the viewport*font-size: 2.5vh;
vminRelative to 1% of viewport’s* smaller dimensionfont-size: 5vmin;
vmaxRelative to 1% of viewport’s* larger dimensionfont-size: 1vmax;
%Relative to the parent elementfont-size: 3%;

Example for Absolute Length Units

In the following example, we set font-size for paragraphs with each of the absolute length units.

index.html

Example for Relative Length Units

In the following example, we set font-size for paragraphs with each of the relative length units.

index.html

Conclusion

In this CSS Tutorial, we learned about length units in CSS, with examples.