Units in CSS
Relative units
Most frequently used in CSS, work well for both print and screen.
%
- percentage, calculated based on a property (usually the same) of parentem
- relative to the font size of the element (or inherited)rem
- relative to font size of the root element
Viewport units
The viewport percentage units are relative to the initial containing block -- usually the browser window.
vw
- 1% of viewport (window) widthvh
- 1% of viewport (window) heightvmin
- 1% of viewport (window) width or height, whichever is smallervmax
- 1% of viewport (window) width or height, whichever is bigger
Units in print
When styling the website for print, you should still use relative units, but viewport units are tricky.
On the other hand, you can use absolute units (aside from px
):
cm
,mm
,in
- centimeter, millimeter, inch
Tasks
See the Pen by t-i-m-i (@t-i-m-i) on CodePen.