Skip to content

Units

Units in CSS are used to define the size of elements. There are a few different types of units in CSS. They are:

  • Absolute Units: These units are fixed and do not change based on the size of the viewport or the parent element. Examples of absolute units include px (pixels), pt (points), and in (inches).

  • Relative Units: These units are relative to the size of the viewport or the parent element. Examples of relative units include % (percentage), em, rem, vw (viewport width), and vh (viewport height).

  • Viewport Units: These units are relative to the size of the viewport. Examples of viewport units include vw (viewport width) and vh (viewport height).

Many different CSS properties accept units, including width, height, font-size, margin, padding, and more. By using different units, you can create flexible and responsive designs that adapt to different screen sizes and devices.

Here are some examples of using different units in CSS:

css
h1 {
	font-size: 36px; /* Absolute unit */
}

p {
	font-size: 1.2em; /* Relative unit */
}

div {
	width: 50%;
}

img {
	width: 100vw; /* Viewport unit */
	height: 50vh; /* Viewport unit */
}

Absolute Units

Absolute units are fixed and do not change based on the size of the viewport or the parent element. They are useful for defining precise measurements and ensuring consistent layouts across different devices and screen sizes.

Here are some examples of absolute units in CSS:

UnitDescription
pxPixels. A pixel is a single dot on a screen
ptPoints. A point is a unit of length commonly used in typography (1pt = 1/72 of 1in)
pcPicas. A pica is a unit of length commonly used in typography (1pc = 12pt)
inInches. An inch is a unit of length equal to 1/12 of a foot (1in = 96px = 2.54cm)
cmCentimeters
mmMillimeters

Who still uses freedom units anyways? I mean, inches and feet? Come on, people!

Relative Units

Relative units are relative to the size of the viewport or the parent element. They are useful for creating flexible and responsive designs that adapt to different screen sizes and devices.

Here are some examples of relative units in CSS:

UnitDescription
%Percentage. A percentage is a relative unit that is calculated based on the size of the parent element
emRelative to the font size of the element
remRelative to the font size of the root element
chRelative to the width of the "0" (zero) character
exRelative to the x-height of the font
vwViewport width. Equal to 1% of the width of the viewport
vhViewport height. Equal to 1% of the height of the viewport
vminEqual to the smaller of vw or vh
vmaxEqual to the larger of vw or vh