dev.rean.me
css

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

Share:

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:

  1. Absolute Units (px): Fixed sizing that does not change based on parent elements or browser zoom settings.
  2. 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

UnitTypeCalculated Relative ToBest Used For
pxAbsolute1 physical screen pixelBorders, fine lines, box-shadows
remRelativeRoot HTML font size (16px default)Font sizes, margins, padding, layout
emRelativeParent / Current element's font sizeButton padding, component scaling
%RelativeParent element's width or heightResponsive grid columns, containers
vw / vhRelative1% of viewport width / heightFullscreen hero sections, modal overlays
dvhRelativeDynamic viewport height (mobile-safe)Mobile fullscreen heroes (no address bar bug)
chRelativeWidth of character 0 in active fontParagraph max-width line lengths
frRelativeFraction of free space in CSS GridGrid template columns and rows

๐Ÿ“ฆ Essential Code Examples

1. px vs. rem vs. em

  • px (Pixel): Absolute value. 16px is always 16px.
  • rem (Root EM): Based on <html> font size (default 1rem = 16px). If user zooms font size in browser settings, rem scales 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:

Button using em padding (Auto-scales with font-size)

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:

Container limited to max-width: 45ch

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!


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)


Happy responsive coding! If you found this short & clear CSS Units guide helpful, feel free to share it with fellow developers! ๐ŸŽจโšก

โ† Back to css
Share: