- There are different types of units that can be defined
Absolute Units
- Always same in any content
px
is an absolute unit because the size of a pixel doesn’t change relative to anything else on the page. In fact, px
is the only absolute unit you should be using for web projects. The rest of them make more sense in a print setting because they are related to physical units such as in
(inch) and cm
(centimeter).
Relative Units
- Can be changed based on their content
em and rem
em
andrem
both refer to a font size, though they are often used to define other sizes in CSS1em
is thefont-size
of an element (or the element’s parent if you’re using it to setfont-size
). So, for example, if an element’sfont-size
is16px
, then setting its width to4em
would make its width64px
(16 * 4 == 64
)1rem
is thefont-size
of the root element (either:root
orhtml
). The math works the same withrem
as it did withem
, but without the added complexity of keeping track of the parent’s font size. Relying onem
could mean that a particular size could change if the context changes, which is very likely not the behaviour you want- The rem unit is only relative to the document’s root element, while the em unit is only relative to the immediate parent of the targeted element
Viewport Units
- The units
vh
andvw
relate to the size of the viewport - Specifically,
1vh
is equal to1%
of the viewport height and1vw
is equal to1%
of the viewport width