TExcelTAD
Home › CSS › CSS Units

CSS Units

Many CSS properties take a length: width, margin, font-size, border-width and more. A length is a number followed immediately by a unit, like 16px or 1.5rem — no space between them. (The only exception: 0 needs no unit.) CSS units fall into two families: absolute and relative.

Absolute units

Absolute units are fixed sizes. On screens, only px is used much; the physical units are meant for print.

Unit Meaning
px Pixel (1/96 of an inch; on high-density screens one CSS pixel spans several device pixels)
pt Point, 1/72 inch — common in print stylesheets
cm, mm, in Centimetres, millimetres, inches
pc Pica, 12pt

Relative units

Relative units scale with something else — the font size, the parent or the viewport — which makes designs flexible and accessible.

Unit Relative to
em Font size of the element itself (for font-size: of the parent)
rem Font size of the root <html> element
% Usually the parent's corresponding size
vw / vh 1% of the viewport width / height
vmin / vmax 1% of the smaller / larger viewport side
dvh / svh / lvh Dynamic / small / large viewport height (handles mobile browser bars)
ch Width of the "0" character in the current font
ex Height of the lowercase "x"
lh Line height of the element
cqw / cqh 1% of a container's width / height (container queries)
fr A fraction of free space (Grid only)

em vs rem

em is relative to the element's own font size, so padding in em grows with the text — great for buttons. But nested em font sizes compound. rem always refers back to the root, so it's predictable.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .btn { display: inline-block; padding: .5em 1em; border-radius: .4em; background: #3949ab; color: white; margin: 4px; }
  .s { font-size: 12px; }
  .m { font-size: 18px; }
  .l { font-size: 28px; }
  .rem-box { width: 10rem; height: 3rem; background: #ffb74d; margin-top: 10px; display: grid; place-items: center; }
</style>
</head>
<body>
  <span class="btn s">Small</span><span class="btn m">Medium</span><span class="btn l">Large</span>
  <p>Same CSS, padding in <code>em</code> scales with each button's font size.</p>
  <div class="rem-box">10rem × 3rem</div>
</body>
</html>

Tip: Use rem for font sizes and layout spacing, em for things that should scale with their own text (button padding, icon size), and px for hairlines like 1px borders.

Percentages and viewport units

Percentages depend on the parent; viewport units depend on the browser window — here, the playground frame. Resize the playground and watch the bars.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; padding: 8px; }
  .parent { width: 60%; background: #eceff1; padding: 4px; }
  .bar { height: 26px; margin: 4px 0; color: white; font-size: 13px; padding-left: 6px; line-height: 26px; }
  .pct { width: 50%; background: #43a047; }
  .vw { width: 50vw; background: #e53935; }
  .title { font-size: 8vw; font-weight: 900; color: #6a1b9a; margin: 6px 0; }
</style>
</head>
<body>
  <div class="parent">
    parent is 60% wide
    <div class="bar pct">50% of parent</div>
    <div class="bar vw">50vw — half the frame</div>
  </div>
  <div class="title">8vw text</div>
</body>
</html>

Character-based units

ch is ideal for readable line lengths and for inputs sized to their expected content.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .zip { width: 7ch; font-family: monospace; font-size: 18px; padding: 4px; }
  .year { width: 6ch; font-family: monospace; font-size: 18px; padding: 4px; }
  .text { max-width: 35ch; background: #fff8e1; padding: 8px; line-height: 1.5; }
</style>
</head>
<body>
  <p>ZIP <input class="zip" value="90210"> Year <input class="year" value="2026"></p>
  <p class="text">This paragraph is limited to about 35 characters per line using the ch unit, so lines stay short.</p>
</body>
</html>

Choosing units

Use case Good unit
Body text, headings rem
Component padding tied to text em
Borders, shadows px
Fluid widths %, fr, or vw
Full-screen sections dvh / vh
Line length ch

Test yourself

Which unit is always relative to the <html> element's font size?

  1. em
  2. rem
  3. vh
  4. %

If the viewport is 800px wide, how wide is 25vw?

  1. 25px
  2. 200px
  3. 400px
  4. 800px

Which is written correctly?

  1. width: 10 px;
  2. width: 10px;
  3. width: px10;
  4. width: "10px";