In this tutorial, weâre going to learn about CSS units!
We use units (px
, em
, rem
, percentages, vw
, vh
etc) to determine the size to apply to properties such as length, padding, margin & align.
Learning about each unit type will help you create more flexible & manageable stylesheets!
What is a CSS Unit?
A CSS unit determines the size of a property youâre setting on an element.
For example, to set the padding
on a paragraph, we could choose 10px
like so:
p {
padding: 10px;
}
The value â10pxâ includes the CSS unit âpxâ (or pixel).
The question is: Is px
the best unit to use? What about em
, or rem
, or vw
?
Letâs find out!
Pixels
.element {
width: 500px;
}
Pixels are the most common measurement unit. Allowing you to set a length in âpixelsâ.
Interestingly, they donât have anything to do with the literal screen pixels of your display device. Itâs a standardization of measurement used across the web.
As weâll see, pixels lack flexibility in some scenarios & there is often a better option.
Percentages
Percentages let you specify values as a percentage, relative to the same property in the elementâs parent. For example:
.parent {
width: 600px;
}
.child {
width: 50%; /* 300px */
}
So if a parent element has a width of 600px, a child element with a width of 50% will render at 300px.
Font-relative units
Em
.element {
width: 30em;
}
em
is the value assigned to the elementâs font-size
, so its exact value changes between elements. The measurement itself the width of the m
letter.
Note that the length changes only when you change the font-size
, it wonât change if the font-family
is adjusted.
By default 1em
is equal to 16px
.
If any CSS changes the font size, 1em becomes equivalent to whatever the new font-size
is.
Rem
.element {
width: 30rem;
}
rem
is similar to em
, only instead of changing based on the font size of the current element, it changes based on the the root (i.e. :root {}
) element font size.
This way you can set font-size
once, and rem
will be a consistent measurement throughout all pages!
ex
.element {
width: 40ex;
}
ex
is similar to em
, however its based on the height of the x
letter.
And unlike ems, it can change depending on both the font-family
used & on the font-size
.
ch
.element {
width: 40ch;
}
ch
is like ex
only instead of measuring the height of x
it measures the width of 0
(the number zero).
It also changes as the font-family
changes.
Viewport units
vw
.element {
width: 20vw;
}
The viewport width unit represents a percentage of the usersâ viewport width.
So 50vw
is equal to 50% of the width of the viewport.
Itâs similar to percentage, only the value remains consistent regardless of the value held by the parent elements. Similar to the way that rem
units remain relative to the root.
vw
units are often used for sizing responsive type.
vh
.element {
width: 20vh;
}
The viewport height unit represents a percentage of the usersâ viewport height.
50vh
equals 50% of the height of the viewport.
This is the same as vw
only itâs based on height instead.
vmin
.element {
width: 10vmin;
}
The viewport minimum is the minimum between the height or width as a percentage.
This value will be whichever is currently smaller, vw
or vh
.
So 20vmin
is 20% of the current width or height, depending which one is smaller.
vmax
.element {
width: 20vmax;
}
The viewport maximum is the maximum between the height or width as a percentage.
Itâs the opposite of vmin
, being whichever value is currently larger, vw
or vh
.
So 20vmax
is the 20% of the current width or height, depending which one is bigger.
Which units should I use?
You can figure out the most appropriate CSS unit by asking yourself:
- Do I want this element to scale when the viewport size changes?
- If âYesâ, what do I want it to scale relative to?
When youâve answered these questions, knowing the most appropriate unit is much simpler!
Related posts:
A little about me..
Hey, Iâm Tim! đ
Iâm a freelance business owner, web developer & author. I teach both new and experienced freelancers how to build a sustainable and successful freelancing business. Check out my Complete Guide to Freelancing if you'd like to find out more.
While you're here, you can browse through my blogs where I post freelancing tips, code tutorials, design inspiration, useful tools & resources, and much more! You can also join the newsletter, or find me on X.
Thanks for reading! đ