Careeroza
CSS Units
Absolute and Relative Units
| Unit | Relative to | Typical use |
|---|---|---|
px | nothing (fixed) | borders, shadows, small details |
% | the parent | widths in fluid layouts |
em | the element's font size | padding that scales with text |
rem | the root (html) font size | font sizes and spacing (recommended) |
vw / vh | 1% of viewport width / height | full-screen sections, fluid type |
ch | width of the "0" character | readable line length: max-width: 65ch |
Example
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
html { font-size: 16px; }
.rem { font-size: 1.25rem; } /* 20px */
.em { font-size: 20px; padding: 1em; background: #f1f1f1; } /* padding 20px */
.vw { font-size: 5vw; color: #04AA6D; } /* changes with the window width */
</style>
</head>
<body>
<p class="rem">1.25rem text</p>
<p class="em">Padding of 1em = this element's font size</p>
<p class="vw">5vw text</p>
</body>
</html>Result