Coding/Learn/css/width height units
CSS Beginner Course
01.Introduction to CSS
02.Selectors (Element, Class & ID)
03.3 Ways to Add CSS to HTML
04.Colors & Backgrounds (HEX, RGB, HSL)
05.Typography & Fonts (font-size, font-weight, line-height)
06.The Cascade & Specificity Wars
07.The Sacred CSS Box Model
08.Padding vs Margin & Shorthand Notation
09.Borders & Border Radius (Rounded Corners & Circles)
10.The Magic of box-sizing: border-box
11.Sizing & Units (px, rem, em, %, vh, vw)
12.The Display Property (block, inline, inline-block, none)
13.Flexbox Part 1: Container Powers (justify, align, gap)
14.Flexbox Part 2: Direction, Wrapping & Item Powers
15.Positioning (relative, absolute, fixed, sticky)
16.Z-Index & The 3D Stacking Context
17.Pseudo-Classes (:hover, :active, :focus, :first-child)
18.Smooth Transitions & Box Shadows
19.Responsive Design & Media Queries (@media)
20.Capstone: Crafting a Modern UI Profile Card
Course Certificate0%

Complete all lessons to earn

LearnSizing & Units (px, rem, em, %, vh, vw)
11 / 20 0%
Lesson 11 of 20
5 mins read

Sizing & Units (px, rem, em, %, vh, vw)

PreviousNext Lesson
In Plain English

CSS has absolute units (px) and relative units (rem, %, vw, vh). For accessible typography that respects user browser zoom, use rem. For responsive containers, use % and max-width. For full-screen layouts, use vh and vw.

The Mental Model

"px is like a wooden ruler marked in exact millimeters (rigid and unbending). rem and % are like rubber bands that stretch and scale gracefully depending on screen size and user eyesight settings."

Deep Dive: How It Works

px (Pixels): Absolute dot size on screen. Great for small borders (1px, 2px) and fixed icons.

rem (Root EM): Relative to the root <html> font size (default is 16px). 1rem = 16px, 1.5rem = 24px, 2rem = 32px. The standard for scalable typography!

% (Percentage): Relative to the parent container's width (e.g. width: 50% takes half the parent).

vw / vh (Viewport Width / Height): 100vw = 100% of browser window width; 100vh = 100% of browser window height.

max-width vs width: width: 800px overflows small mobile screens; max-width: 800px shrinks automatically on phones!

Syntax Blueprint

/* Scalable Typography */
h1 { font-size: 2.25rem; } /* 36px */

/* Responsive Container */
.container {
  width: 100%;
  max-width: 1200px;
  min-height: 100vh;
}

Use rem for typography and percentage with max-width for responsive shells.

PointCore Rules to Remember

Point star
rem for Text: Always use rem for font-size so visually impaired users can increase browser font size.
Point star
max-width > width: Always prefer max-width on containers so mobile screens don't break horizontally.
Point star
100vh for Full Screen: min-height: 100vh ensures hero sections fill the full vertical screen.

Common Beginner Traps & How to Fix Them

Mistake:Using fixed width: 1000px on body containers

Why it happens: Testing only on desktop monitors.

How to fix: Use max-width: 1000px; width: 100%; so it fluidly shrinks on smartphones.

Pro Tip: 1rem = 16px. To calculate rems easily: Divide your pixel target by 16 (e.g. 24px / 16 = 1.5rem)!

Live Interactive Example

Hit Run Code to see it live
Responsive Units in Action
CSS3
1
2
3
4
5
6
7
8
9
10
11
12
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: The layout scales smoothly across desktop, tablet, and mobile screens.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create a fluid responsive container

  • PointWrite a CSS rule for .main-content with width: 100%; max-width: 800px; and font-size: 1.125rem;
1
2
Browser Preview & Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates