Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Đơn vị tương đối (Relative Units) trong CSS

Giới thiệu

Đơn vị tương đối (relative units) xác định kích thước dựa trên ngữ cảnh (parent element, root element, viewport, font-size...). Khác với đơn vị tuyệt đối (px, pt, cm...), đơn vị tương đối cho phép layout responsive, accessible, scalable.

Bảng tổng hợp đơn vị tương đối

Đơn vịViết tắtCơ sở tính toánDùng cho
Percent%Kích thước của parent elementWidth, height, margin, padding, font-size (ít dùng)
EmemFont-size của parent elementFont-size, padding, margin, width/height component
ExexChiều cao chữ 'x' của font hiện tạiFine-tuning vertical rhythm, ít dùng
Root EmremFont-size của root element (<html>)Font-size toàn cục, spacing system, responsive typography
Viewport Widthvw1% chiều rộng viewportFull-width layout, fluid typography, hero section
Viewport Heightvh1% chiều cao viewportFull-height section, modal, sticky footer
Viewport Min/Maxvmin / vmax1% min/max (width, height) viewportResponsive square, aspect ratio, fluid type scale
CharacterchChiều rộng ký tự '0' (zero) của font hiện tạiInput width, text container, reading line length
GridgdGrid track size (CSS Grid)Grid layout (hiếm dùng trực tiếp)

So sánh: em vs rem

<style>
  html { font-size: 16px; }           /* Root = 16px */
  .parent { font-size: 20px; }        /* Parent = 20px */
  
  .child-em  { font-size: 1.5em; }    /* 1.5 × 20px = 30px (kế thừa parent) */
  .child-rem { font-size: 1.5rem; }   /* 1.5 × 16px = 24px (luôn root) */
</style>

Khi nào dùng đơn vị nào?

  • rem: Font-size global, spacing system (margin, padding), media query breakpoint — khuyên dùng chính
  • em: Font-size component cục bộ, padding/margin internal component (scale theo font component)
  • %: Width/height layout fluid, flex/grid child sizing
  • vw/vh: Hero full-screen, fluid typography (clamp(1rem, 2.5vw, 2rem))
  • ch: Độ rộng input, max-width đọc thoải mái (max-width: 65ch)
  • vmin/vmax: Kích thước vuông responsive, scale theo màn hình nhỏ hơn

Best Practice hiện đại

/* Fluid typography với clamp() */
html { font-size: clamp(14px, 1.2vw, 18px); }

/* Spacing system dựa trên rem */
:root { --space: 1rem; }
.m-1 { margin: var(--space); }
.p-2 { padding: calc(var(--space) * 2); }

/* Container responsive */
.container { width: min(100% - 2rem, 1200px); margin-inline: auto; }