html
/

CSS Units

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

html

CSS Units

What are CSS Units?

CSS units define the size of elements, text, spacing, and layout. They are categorized into absolute and relative units.

Absolute Units

Absolute units have fixed values and do not change based on screen size.

CSSRead-only
1
p {
  font-size: 16px;
  width: 200px;
}

Common Absolute Units

UnitDescription
pxPixels (most common)
cmCentimeters
mmMillimeters
inInches
ptPoints

Relative Units

Relative units adjust based on parent elements or screen size, making them ideal for responsive design.

CSSRead-only
1
p {
  font-size: 1.5em;
}

.container {
  width: 50%;
}

Common Relative Units

UnitDescription
%Relative to parent element
emRelative to parent font size
remRelative to root font size
vwViewport width (1vw = 1% of screen width)
vhViewport height (1vh = 1% of screen height)

Example Comparison

CSSRead-only
1
h1 {
  font-size: 2rem;
}

.box {
  width: 50vw;
  height: 30vh;
}

em vs rem

UnitBased On
emParent element
remRoot (html) element

Best Practices

  • Use rem for consistent typography
  • Use % for flexible layouts
  • Use vw/vh for responsive designs
  • Avoid overusing px for scalability

Common Mistakes

  • Using only px for all layouts
  • Confusing em with rem
  • Ignoring responsive units
  • Mixing units inconsistently

Conclusion

CSS units play a crucial role in responsive design. Choosing the right unit ensures flexibility and better user experience across devices.

Try it yourself

h1 {
  font-size: 2rem;
}

.box {
  width: 50vw;
}

Test Your Knowledge

Q1
of 3

Which unit is fixed?

A
%
B
em
C
px
D
rem
Q2
of 3

Which unit is relative to root?

A
em
B
rem
C
%
D
vh
Q3
of 3

Which unit uses viewport?

A
px
B
em
C
vw
D
pt

Frequently Asked Questions

What is px?

A fixed unit representing pixels.

What is rem?

Relative to the root font size.

Which unit is best for responsive design?

Relative units like %, rem, vw, vh.

Previous

css colors

Next

css background

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.