CSS Units Explained: Short & Clear Guide with Examples (px, rem, em, %, vw, vh, ch), Tips & Related Posts
Master CSS units fast with short, clear visual examples. Learn absolute vs relative units (px vs rem vs em vs %), viewport units (vw, vh, dvh), practical UI tips, and related posts opening in a new tab.
2026-10-02 ยท 6 min read
CSS offers a wide variety of measurement units to control element sizing, spacing, margins, padding, and font sizes. Choosing the right CSS unit is critical for building responsive, accessible, and device-independent user interfaces.
Here is a short, clear, and visual guide to CSS units. ๐
๐ก Core Concept: Absolute vs. Relative Units
CSS units are divided into two main categories:
- Absolute Units (
px): Fixed sizing that does not change based on parent elements or browser zoom settings. - Relative Units (
rem,em,%,vw,vh,ch,fr): Dynamic sizing calculated relative to another value (browser font size, parent container, or viewport dimensions).
โก Quick Cheatsheet
| Unit | Type | Calculated Relative To | Best Used For |
|---|---|---|---|
px | Absolute | 1 physical screen pixel | Borders, fine lines, box-shadows |
rem | Relative | Root HTML font size (16px default) | Font sizes, margins, padding, layout |
em | Relative | Parent / Current element's font size | Button padding, component scaling |
% | Relative | Parent element's width or height | Responsive grid columns, containers |
vw / vh | Relative | 1% of viewport width / height | Fullscreen hero sections, modal overlays |
dvh | Relative | Dynamic viewport height (mobile-safe) | Mobile fullscreen heroes (no address bar bug) |
ch | Relative | Width of character 0 in active font | Paragraph max-width line lengths |
fr | Relative | Fraction of free space in CSS Grid | Grid template columns and rows |
๐ฆ Essential Code Examples
1. px vs. rem vs. em
px(Pixel): Absolute value.16pxis always16px.rem(Root EM): Based on<html>font size (default1rem = 16px). If user zooms font size in browser settings,remscales automatically!em(Element EM): Based on the font size of the current or parent element.
html {
font-size: 16px; /* 1rem = 16px */
}
.title {
font-size: 2rem; /* 2 * 16px = 32px */
margin-bottom: 1rem; /* 1 * 16px = 16px */
}
.button {
font-size: 14px;
padding: 0.5em 1em; /* Padding scales proportionally with button font size! */
}
โก Live Result Preview:
2. Viewport Units: vw, vh, and dvh
100vw: 100% of browser window width.100vh: 100% of browser window height (may cause mobile browser URL bar clipping).100dvh: 100% of Dynamic Viewport Height (adapts smoothly when mobile URL bar shows or hides).
.hero-section {
width: 100vw;
min-height: 100dvh; /* Mobile-safe full screen height */
display: flex;
justify-content: center;
align-items: center;
}
3. Character Unit (ch) for Optimal Line Length
1ch equals the width of the 0 character in the current font.
.blog-paragraph {
max-width: 60ch; /* ~60 characters wide for maximum reading comfort */
margin-inline: auto;
}
โก Live Result Preview:
Restricting paragraph containers with ch units ensures text lines never stretch too far across widescreen monitors!
4. Fluid Scaling with clamp(min, preferred, max)
Combines relative units (vw, %) with minimum and maximum limits for fluid responsiveness.
.responsive-title {
/* Min: 1.5rem (24px), Preferred: 4vw, Max: 3rem (48px) */
font-size: clamp(1.5rem, 4vw, 3rem);
}
๐ฏ 5 Essential CSS Unit Pro Tips
Tip 1: The Golden Typography Rule: Use rem for Fonts
Avoid hardcoding px for font-size. If visually impaired users increase default text size in their browser settings, hardcoded px fonts will ignore their preferences, violating accessibility standards!
Tip 2: Quick rem Conversion Cheat Sheet (Base 16px)
Keep this mental multiplier reference handy:
8px = 0.5rem
12px = 0.75rem
14px = 0.875rem
16px = 1rem (Base default)
20px = 1.25rem
24px = 1.5rem
32px = 2rem
48px = 3rem
Tip 3: Use px Exclusively for Borders & Fine Shadows
Borders should remain crisp 1px or 2px thin lines on all devices. Do not convert border widths to rem or %.
Tip 4: Always Use dvh Over 100vh on Mobile Layouts
Standard 100vh on mobile browsers (Safari iOS & Chrome Android) includes the hidden address bar height, causing bottom elements to get cut off. 100dvh dynamically adjusts height when scrolling!
Tip 5: Use em for Component Internal Padding
When building reusable buttons or badges, use em for padding (e.g. padding: 0.5em 1em). Changing the component's font-size will automatically scale its padding proportionally!
๐ Related Posts & Useful Resources
Explore these official specifications and related CSS guides on our platform. All external and internal reference links open in a new tab.
๐ Official CSS Specs & Guides (New Tab)
- MDN Web Docs โ CSS Values and Units Complete Guide
- W3C CSS Values and Units Module Level 4 Specification
- Modern CSS โ Component Spacing & Scaling with em Units
๐ท๏ธ Related CSS Tutorials on dev.rean.me (New Tab)
- CSS Fonts & Text Styling Explained: Short & Clear Guide with Examples
- CSS Media Queries Explained: Short & Clear Guide with Examples
- CSS Flexbox Explained: Short & Clear Guide with Examples & Pro Tips
- CSS Grid Explained: Short & Clear Guide with Examples & Pro Tips
- CSS Positioning Explained: Static, Relative, Absolute, Fixed & Sticky
- CSS Cheat Sheet: Essential Syntax, Box Model & Properties
- CSS Tips and Tricks with Practical Code Examples
- Complete CSS Property & Selector Reference Guide
Happy responsive coding! If you found this short & clear CSS Units guide helpful, feel free to share it with fellow developers! ๐จโก