Reference
- MDN: Values and Unites
Best practices
Classic units
px- 96 pixels = 1 “inch”
- Defined in the core browser settings. It’s rarely 1 inch.
- Often only used for:
- Setting
font-size; - Setting image
widthandheight; - Setting a
1pxborder.
- Setting
- 96 pixels = 1 “inch”
%- Represents a fraction of some other value. For example, when defined for
width, it’s a fraction of the parent element’s width. -
Most often used for repnsive images:
img { max-width: 100%; }
- Represents a fraction of some other value. For example, when defined for
Font-based units
em- If setting
font-size:1emis equal to thefont-sizeof the parent element.- Avoid setting
font-sizeinemunits. Usepxorreminstead.
- Avoid setting
- If setting a length for another property:
1emis equal to thefont-sizeof the element itself.
- If setting
rem- Font size of the root element.
- When in doubt, no one got fired for using
remunits. - See: em vs rem
ch- The advance measure (width) of the glyph “0” of the element’s font.
- Tony loooooves setting setting text boxes in
chunits. - Warning: the value of
chchanges with differentfont-familydeclarations. Don’t usechif you need a consitent width. - See: Length comparison: rem vs ch
Viewport
vh,vw,vmin,vmax- Used for global page layout.
- They lose their effectiveness for local layouts.
