- 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
emandremboth refer to a font size, though they are often used to define other sizes in CSS1emis thefont-sizeof an element (or the element’s parent if you’re using it to setfont-size). So, for example, if an element’sfont-sizeis16px, then setting its width to4emwould make its width64px(16 * 4 == 64)1remis thefont-sizeof the root element (either:rootorhtml). The math works the same withremas it did withem, but without the added complexity of keeping track of the parent’s font size. Relying onemcould 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
vhandvwrelate to the size of the viewport - Specifically,
1vhis equal to1%of the viewport height and1vwis equal to1%of the viewport width