Different Units in CSS


CSS has several measurement units for length and size. These measuring units are used to specify a non-zero value to the CSS properties.

CSS units are used where we need to define size, length, etc. EX - height, width, border-size, padding, font-size etc.

CSS unit values are specified by a numerical value followed by a unit and there should be no space between number and unit. For example:


Types of CSS Units

CSS units can be categorized into two types:

  1. Absolute unit
  2. Relative unit

1 .Absolute unit

These units are fixed and do not change with the size of the viewport. These units are used to specify the size of the element in the document. EX - height, width, border-size, padding, font-size etc.

Absolute units are independent of its parent element.

Device to device screen size varies so absolute length units are not recommended. It is dependent on an output medium. It can be used when the desired output is known.

These are absolute units with their description:

UnitsDescription
pxPixel is the smallest unit of measurement in CSS.
cmCentimeter is a unit of length in the metric system.
mmMillimeter. 1mm = 1/10th of 1cm.
inInch is a unit of length in the imperial system. 1in = 2.54cm.
ptPoint is a unit of length in typography. 1pt = 1/72 of 1in.
pcPica is a unit of length in typography. 1pc = 12pt.
CSS absolute units example
Change the values from options to see the effect.
.px { font-size: 1px; }
.mm { font-size: 1mm; }
.cm { font-size: 1cm; }
.in { font-size: 1in; }
.pt { font-size: 10pt; }
.pc { font-size: 10pc; }
Try it

2. Relative unit

The relative unit is a unit of measurement that is relative to another value. The value is relative to the parent element. This means if the parent element is changed, the child element will change accordingly.

Here is the list of relative units:

UnitsDescription
emIt is relative to the current font size of the element. 1em means the current font size, 2em means twice the current font size, 0.5em means half the current font size.
remIt is relative to the font size of the root element. 1rem means the font size of the root element, 2rem means twice the font size of the root element
exIt is relative to the x-height of the current font. 1ex means the x-height of the current font.
chIt is relative to the width of the glyph "0" (width of zero) of the current font. 1ch means the width of the glyph "0" (zero) of the current font.
vwIt is relative to 1% of the width of the viewport. 1vw means 1% of the width of the viewport.
vhIt is relative to 1% of the height of the viewport. 1vh means 1% of the height of the viewport.
vminIt is relative to 1% of the smaller of the viewport's height and width. 1vmin means 1% of the smaller of the viewport's height and width.
vmaxIt is relative to 1% of the larger of the viewport's height and width. 1vmax means 1% of the larger of the viewport's height and width.
%It is relative to the parent element. 1% means 1% of the parent element.
CSS relative units example
Change the values from options to see the effect.
.box {
  width: 60vw;
}

.em-font {
  font-size: 2em;
}
Try it