MyInternships.in

CSS References

CSS Units Reference

CSS units define the magnitude of lengths — widths, margins, font sizes and more. They fall into two families: absolute units, which are fixed physical sizes, and relative units, which scale with something else such as the font size, the parent, or the viewport. Choosing the right unit is key to responsive, accessible design. This reference lists them all.


Units at a glance

UnitDescriptionType
pxPixels — a fixed device-independent dot.Absolute
ptPoints — 1/72 inch, mainly for print.Absolute
pcPicas — 12 points.Absolute
inInches — 96px.Absolute
cmCentimetres.Absolute
mmMillimetres.Absolute
emRelative to the font-size of the element.Relative
remRelative to the root (<html>) font-size.Relative
%Relative to the parent's corresponding value.Relative
chWidth of the '0' character of the current font.Relative
exThe x-height of the current font.Relative
vw1% of the viewport width.Relative
vh1% of the viewport height.Relative
vmin1% of the smaller viewport dimension.Relative
vmax1% of the larger viewport dimension.Relative
vi1% of the viewport size in the inline direction.Relative
vb1% of the viewport size in the block direction.Relative
svh / lvh / dvhSmall, large and dynamic viewport height (mobile-aware).Relative
frA fraction of free space in a CSS Grid.Relative (grid)
lhThe line-height of the element.Relative
rlhThe line-height of the root element.Relative
cqw / cqh1% of a query container's width / height.Relative (container)
degDegrees, for angles (transforms, gradients).Angle
s / msSeconds / milliseconds, for time.Time

Absolute vs relative — when to use which

  • Use rem for font sizes and spacing so everything scales with the user's browser font-size setting — the accessible choice.
  • Use em when you want a value to scale with the element's own font size, e.g. padding inside a button.
  • Use % and fr for flexible layout widths and grid tracks.
  • Use vw/vh (or dvh) for full-viewport heroes and fluid typography with clamp().
  • Use ch to size text columns to a comfortable line length (around 60–75ch).
  • Reserve px for hairline borders and places where a fixed size is genuinely intended.

em vs rem — the key difference

em compounds: a nested element with font-size: 1.2em multiplies its parent's size, so deep nesting can snowball. rem always refers to the root font-size, so it stays predictable no matter how deeply nested the element is.

:root { font-size: 16px; }
h1  { font-size: 2rem; }    /* always 32px */
.box{ font-size: 1.25em; } /* 1.25 x its parent's font-size */
.wrap { width: min(90vw, 60ch); }
💡

On mobile, prefer dvh over vh for full-height sections — dvh accounts for the browser's collapsing address bar, avoiding content that gets cut off.

Related CSS Topics

Keep learning with these closely related tutorials.

Ready to use your CSS skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships