In CSS, size matters. Whether you are setting the width of a button, the size of a font, or the spacing between sections, you need to use Measurement Units. These units are divided into two main categories: Absolute and Relative.
1. Absolute Units
Absolute units are fixed. A size defined in absolute units will appear as exactly that size, regardless of the screen size or settings. These are generally not recommended for responsive layouts but are useful for print media.
| Unit | Name | Description |
px | Pixels | The most common unit. 1px = 1/96th of an inch. |
cm | Centimeters | Standard physical centimeters. |
mm | Millimeters | Standard physical millimeters. |
in | Inches | 1 inch = 96px = 2.54cm. |
pt | Points | 1 point = 1/72nd of an inch (commonly used in print). |
Q | Quarter-millimeters | 1Q = 1/40th of 1cm |
pc | picas | 1pc = 1/6th of 1in |
Example Code
p {
font-size: 40px; /* Fixed size on all screens */
margin-bottom: 2cm;
}
Output
Paragraph 40px
2. Relative Units
Relative units calculate size based on another value, such as the parent element’s font size or the size of the browser window (viewport). These are essential for modern, responsive web design.
A. Font-Relative Units
em: Relative to the font-size of the element itself. (If font-size is 16px,2em= 32px).rem: Relative to the Root (the<html>element) font-size. This is much more predictable thanem.ch: Relative to the width of the “0” character in the current font.
B. Viewport-Relative Units
vw: Viewport Width.1vwis equal to 1% of the width of the browser window.vh: Viewport Height.1vhis equal to 1% of the height of the browser window.vmin/vmax: Relative to the smaller or larger dimension of the viewport.
C. Percentage
%: Relative to the parent element’s size.
| Unit | Description | Example |
em | Relative to the font-size of the element. | font-size: 6em; |
rem | Relative to font-size of the root element. | font-size: 6rem; |
ch | Relative to width of the “0”. | font-size: 6ch; |
ex | Relative to the x-height of the current font. | font-size: 4ex; |
lh | It is relative to the line height of the element. | font-size: 6lh; |
rlh | It is relative to the line height of the root element. | font-size: 6vh; |
vw | It is relative to the height of the viewport. 1vh = 1% or 1/100 of the height of the viewport. | width: 6vw; |
vh | It is relative to the width of the viewport. 1vw = 1% or 1/100 of the width of viewport. | font-size: 6vh; |
vmin | It is relative to the smaller dimension of the viewport. 1vmin = 1% or 1/100 of the viewport’s smaller dimension. | width: 6vmin; |
vmax | It is relative to the larger dimension of the viewport. 1vmax = 1% or 1/100 of the viewport’s larger dimension. | width: 6vmax; |
vb | It is relative to the size of the initial containing block in the direction of the root element’s block axis. 1vb = 1% of containing block’s size (block axis). | font-size: 6vb; |
vi | It is relative to the size of the initial containing block in the direction of the root element’s inline axis. 1vb = 1% of containing block’s size (inline axis). | font-size: 6vi; |
svw, svh | It is relative to the size of the initial containing block in the direction of the root element’s inline axis. 1vb = 1% of containing block’s size (inline axis). | width: 40svw; height: 40svh; |
lvw, lvh | It is relative to the width and height of the larger viewport. 1lvw = 1% or 1/100 of the larger viewport’s width and 1lvh = 1% or 1/100 of the larger viewport’s height. | width: 40lvw; height: 40lvh; |
dvw, dvh | It is relative to the width and height of the dynamic viewport. 1dvw = 1% or 1/100 of the dynamic viewport’s width and 1dvh = 1% or 1/100 of the dynamic viewport’s height. | width: 40dvw; height: 40dvh; |
Example Code
.r-container {
width: 80%; /* 80% of the parent element */
padding: 2rem; /* Based on root font size (usually 16px * 2) */
}
.r-hero-text {
font-size: 5vw; /* Text gets bigger on wider screens */
}
Output
Output Text
3. When to Use Which Unit?
- Use
pxfor borders or elements that should stay a constant size (like a logo). - Use
remfor font sizes and spacing to ensure accessibility (it respects user browser settings). - Use
%orvw/vhfor layout widths and heights to ensure the site fills the screen correctly on mobile and desktop.