Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - CSS Units


CSS Units

CSS uses different units to define sizes for margins, padding, fonts, and layout elements. Understanding how each unit works helps you create flexible, responsive, and consistent designs.


Absolute vs Relative Units

CSS units are divided into two categories:

  • Absolute units - fixed sizes that don't change (e.g., px).
  • Relative units - scale relative to another value (e.g., parent size or viewport).

Absolute Units

Absolute units always appear the same size, regardless of screen or zoom level.

UnitDescriptionExample
pxPixels (most common unit)width: 200px;
cmCentimeterswidth: 5cm;
mmMillimetersheight: 50mm;
inInches (1in = 96px)width: 1in;
ptPoints (1pt = 1/72 inch)font-size: 12pt;
pcPicas (1pc = 12pt)font-size: 1pc;

Note: Absolute units are not recommended for responsive design because they don't scale well on different screens.


Relative Units

Relative units change depending on the parent element, font size, or viewport. They are essential for responsive layouts.

UnitDescriptionExample
%Relative to parent elementwidth: 50%;
emRelative to the current element's font sizepadding: 2em;
remRelative to the root (HTML) font sizefont-size: 1.5rem;
vw1% of the viewport widthfont-size: 5vw;
vh1% of the viewport heightheight: 100vh;
vmin1% of the smaller dimension (width or height)font-size: 3vmin;
vmax1% of the larger dimensionwidth: 80vmax;

Using Percentages

Percentages are based on the parent element's size, making them ideal for flexible layouts.

Example

Percentage-based width:

div.container {
  width: 80%;
  margin: auto;
  background-color: lightgray;
}
Try it Yourself »

Using em and rem

em and rem units are commonly used for font sizes and spacing:

  • 1em = current element's font size.
  • 1rem = root element's font size (usually 16px by default).

Example

em vs rem:

html { font-size: 16px; }
div { font-size: 20px; }
p.one { font-size: 1em; } /* 20px */
p.two { font-size: 1rem; } /* 16px */
Try it Yourself »

Viewport Units

Viewport units are relative to the browser window size. They are great for responsive typography and full-screen sections.

Example

Full-screen section using viewport height:

section {
  height: 100vh;
  background-color: lightblue;
}
Try it Yourself »

Combining Units

You can mix units to create flexible designs - for example, using fixed padding with percentage widths.

Example

Mixed units:

.container {
  width: 80%;
  padding: 20px;
  font-size: 1.2rem;
}
Try it Yourself »

Best Practices

  • Use px for fine control in icons or borders.
  • Use %, em, or rem for scalable layouts.
  • Use vh and vw for full-screen sections and responsive text.
  • Set html { font-size: 100%; } for accessibility and rem consistency.

Summary

  • Absolute units (like px) have fixed sizes.
  • Relative units (%, em, rem, vh, vw) scale dynamically.
  • Use relative units for responsive designs.
  • Viewport units help elements fill the screen smoothly.

Next, you'll learn about CSS Variables - how to store reusable values and make your stylesheets easier to manage and maintain.

Next » CSS Variables


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.