RELATIVE UNITS /// EM /// REM /// VW /// VH /// RELATIVE UNITS /// EM /// REM /// VW /// VH /// RELATIVE UNITS ///

Relative Units

Say goodbye to hardcoded pixels. Master em, rem, viewports, and percentages to create layouts that flow like water across any device.

styles.css
1 / 13
12345
📏

Tutor:Hardcoding pixel values (px) breaks responsive design. We need Relative Units to build adaptable, accessible interfaces.


Skill Matrix

UNLOCK NODES BY MASTERING FLUIDITY.

Concept: em & rem

Typography scaling units. em scales with the parent element, while rem scales with the root HTML font size.

System Check

Which unit is safer to prevent exponential font compounding?


Community Holo-Net

Showcase Your Layouts

ACTIVE

Built a perfectly fluid responsive grid? Share your codepens and get feedback!

CSS Relative Units: Fluid Foundations

Author

Pascual Vila

Frontend Instructor // Code Syllabus

In modern web design, devices range from tiny smartwatches to 4K TVs. Hardcoding pixel values is a guaranteed way to break layouts. Relative units are the secret to writing CSS that adapts gracefully to any environment.

The Problem with Pixels (px)

Pixels are absolute units. A 16px font size will always be exactly 16 CSS pixels, regardless of user preferences or screen size. This poses a massive accessibility (a11y) issue. If a visually impaired user sets their browser default font size to 24px, a hardcoded 16px rule will override their settings, making your site unreadable.

em vs rem: The Typography Kings

Both em and rem scale based on font size, but they do it differently:

  • em: Relative to the parent element's font-size. If used for margins or padding, it's relative to the element's own font-size. It's great for isolated components (like a button where the padding scales with its text), but terrible for layout because it compounds.
  • rem (root em): Always relative to the <html> element's font-size. 1rem is usually 16px by default. It never compounds, making it perfect for consistent spacing and typography across your entire app.

Viewport Units: Painting the Canvas

vw (viewport width) and vh (viewport height) represent 1% of the browser's current window size. They are completely independent of HTML structure or parent containers.

Want a hero section to cover the whole screen? height: 100vh; width: 100vw;. They are also amazing for fluid typography when combined with CSS clamp() (e.g., font-size: clamp(1rem, 5vw, 3rem);).

Pro Tip: The 62.5% Trick+

Some developers set html { font-size: 62.5%; }. Since the default is 16px, 62.5% of 16 is 10px. This makes math incredibly easy: 1.6rem = 16px, 2.4rem = 24px. It keeps the math simple while fully respecting user accessibility overrides!

Frequently Asked Questions

When should I use % vs vw?

Use % when you want an element to size itself relative to its parent container. Use vw when you want an element to size itself relative to the entire browser window, regardless of what container it is inside.

Is it bad to use px at all?

Not necessarily! px is totally fine for things that shouldn't scale, like 1px borders, subtle box-shadows, or small fixed icons. Just avoid it for typography and layout containers.

What are vmin and vmax?

vmin is 1% of the smaller dimension of the viewport (either width or height). vmax is 1% of the larger dimension. They are amazing for keeping elements square and visible on both landscape and portrait device orientations.

Units Glossary

em
Relative to the font-size of the element's parent. If used on the font-size property itself, it's relative to the parent. If used on other properties (like margin), it's relative to the element's own font-size.
css
rem
Root em. Relative strictly to the font-size of the root element (<html>). Highly recommended for global typography scaling.
css
% (Percentage)
Relative to the dimensions of the parent container. Commonly used for widths, heights, and flexbox bases.
css
vw / vh
Viewport Width / Viewport Height. 1vw is 1% of the browser window's width. 1vh is 1% of the height.
css
vmin / vmax
Viewport Minimum / Viewport Maximum. 1vmin is 1% of whichever is smaller (width or height).
css
ch
Character unit. Relative to the width of the "0" (zero) character in the element's current font. Great for setting maximum reading widths.
css