CSS units define how we specify sizes on websites, such as font size, width, or height. They fall into two main categories: absolute and relative units.
Absolute units are fixed values that provide the same size on all devices (for example px or cm). Relative units, on the other hand, adapt to the environment, such as the current font size or the size of the browser window.
Unit | Type | Description |
---|---|---|
px | Absolute | Pixel, the most commonly used absolute unit in web development. |
cm | Absolute | Centimeter, rarely used, mainly in print display. |
mm | Absolute | Millimeter, also used for printing purposes. |
em | Relative | A unit relative to the font size of the current element. |
rem | Relative | A unit relative to the font size of the root (<html>) element. |
% | Relative | Percentage, defines value relative to the size of the parent element. |
vh | Relative | Represents 1% of the height of the browser window. |
vw | Relative | Represents 1% of the width of the browser window. |
Let us look at some examples of how absolute and relative units work in practice.
In this example, one paragraph has an absolute (px) font size, while the other uses a relative (em) font size.
In this example, a box has a width of 50% of its parent element and a height of 50% of the viewport height.
Viewport units adapt to the size of the browser window. 100vw represents the full width, while 100vh represents the full height. This is especially useful for creating full-screen sections.
The em unit relates to the font size of the current parent element, while rem always relates to the root (html) element. Therefore, 2em and 2rem can produce different sizes.
In practice, relative units are often combined: for example, a box can be 80% wide and 60vh high, with an inner element inside it that is 50% wide and 50% high.
The correct use of units is crucial when creating responsive and user-friendly websites.
Please sign in to ask Lara about CSS Units.
Select Language
Set theme
© 2025 ReadyTools. All rights reserved.