DEV Community

Milecia
Milecia

Posted on • Updated on

Most Common CSS Units Of Measure Explained

When you look through CSS and you start seeing the different units of measure, it's helpful to know exactly what they do. To help you better understand how the CSS units of measure work, here are some explanations and examples of how the most commonly used ones work.

px

Pixels (px) are the most commonly used unit of measure because there are many standards in web design based around the pixel. Just for your own knowledge, a px maps to one pixel on your screen. It's super small, but the point of having this reference is to know exactly how much physical space an element or text will fill.

When you are using pixels, keep in mind that these units change relative to the size of the screen. 12px is going to look a little different depending on whether it's on a phone or a laptop . Pixels form the base measurement of websites. The default root size of a web page is set to 16px by all of the popular browsers.

div {
    height: 32px;
}
Enter fullscreen mode Exit fullscreen mode

%

Percentages get a lot of use in responsive design and are pretty straightforward. The one thing you have to remember is that the size is relative to the parent element. If you have a <h2> nested in a <div> and you give it a width using the % unit of measure, then the <h2> width will be relative to the size of the <div>. It'll look something like this:

div {
    width: 1250px;
}

h2 {
    width: 35%;
}
Enter fullscreen mode Exit fullscreen mode

Now you know that the width of the <h2> will be 35% of the <div> width. Percentages are more simple than the em and rem units, but percentages aren't typically used for properties like font-size or border so there are more limited uses of it.

em

This is relative to the font-size value of the element. So say you have a <p> inside of a <div> and these are the styles you have:

div {
    font-size: 3em;
}

p {
    width: 2em;
}
Enter fullscreen mode Exit fullscreen mode

The <div> will have a font size of 3 times the font-size of the <body>. So that will give it a default of 48px. That number comes from the default font size on browsers which is 16px like we discussed earlier. With the <div> being 3 times that, you get the 48px.

That means your <p> is going to have a width that is 2 times bigger than the font-size of the <div>. So your <p> would have a width of 96px.
That's how the em unit of measure works. Remember that the font-size value of the containing element determines the size of a child element using em.

rem

This unit of measure is really similar to the em unit of measure except it is easier to understand. The rem unit of measure is based off of the font-size of the root element. Unless you change the <body> font-size, this base size will be 16px.

That means everything that has the rem unit behind it is just a multiple of 16px. So for example, let's say you have a sidebar and a main container on your page. Here's your CSS:

.sidebar {
    width: 2.4rem;
}

.main-container {
    width: 7.2rem;
}
Enter fullscreen mode Exit fullscreen mode

Since you used rem, you should be able to easily calculate how big your elements will be. This is a big advantage over em because you don't have to keep up with the font-size of every element and it still does a little scaling when the page is looked at on different devices.

I hope that this quick explanation helped you understand the common CSS units of measure better!

These are just the most commonly used CSS units of measure, not the only ones. There are at least 11 others. The problem with some of the others are their limited browser support.

Two of the other units of measure that are gaining some popularity are the viewport measures. The viewport height and viewport width, vh and vw, make it even easier to make responsive layouts because they are both relative to the size of the screen. But that's a discussion for another day.


Hey! You should follow me on Twitter because reasons: https://twitter.com/FlippedCoding

Top comments (15)

Collapse
 
willemodendaal profile image
Willem Odendaal

How can pixels have a constant relation to inches?
A pixel at 1080p on my 17" laptop is surely much smaller (physically) than a pixel at 1080p on my 50" tv?

Collapse
 
bitifet profile image
Joan Miquel Torres • Edited

px aren't pixels.

They are more like an "average pixel" that browsers should scale to real pixels.

This way they are much similar to actual pixels in which developers are accustomed to think in while mantaining their average real size across devices.

Collapse
 
willemodendaal profile image
Willem Odendaal

I read a bit more about this, and it seems that a Joan is correct. A "CSS pixel" is different from a hardware pixel.

"A CSS pixel, on the other hand, is designed to be roughly equivalent across devices. If you load the same website on side-by-side devices with a similar physical dimensions, but different pixel ratios, the website will appear to be roughly the same visual size."

A good read: juiceboxinteractive.com/blog/a-pix...

Collapse
 
dploeger profile image
Dennis Ploeger

I was immediately confused as well. I guess, the author got that one pretty wrong.

Collapse
 
flippedcoding profile image
Milecia

Sorry for the confusion guys. I was thinking pt and typing px. D'oh! But I've corrected it above.

Just for clarity, px maps to a pixel on your screen. Pt are the units that are 1/72 of an inch.

Collapse
 
blamrob profile image
💥Blam

I usually explain px by saying to think of it as "1/72" of an inch. Prior to "retina" and other pixel dense screens, it was simply one pixel on the screen. However it doesn't really mean that anymore.

I still encounter people who were told never to use px. However, if you use the "computed" panel in the web inspector, you can see that, ultimately, all units of measurement get converted to pixels anyway. That doesn't mean that other units don't have their advantages, but I do tend to use pixels most of the time. With a few exceptions.

Collapse
 
dance2die profile image
Sung M. Kim

Thanks Milecia for the post.

I always get confused between rem & em and the post made it clearer for me.

Collapse
 
juniusfree1 profile image
juniusfree

When do you use a specific measure over the others? Thanks!

Collapse
 
flippedcoding profile image
Milecia

A lot of it comes down to preference, but usually percentages are used for block level elements like <div>.

If you need something that's a fixed size you'd probably go with px.

The others are just a toss up.

Collapse
 
email_vijayr profile image
Vijayr • Edited

I guess vh and vw measures will be more helpful because it adjust based on screen size. But yes, each measure have it's own importance.

Collapse
 
everburningfire profile image
Runcibald Spooner

Thanks for the explanation. It was simply put and understandable.

Collapse
 
email_vijayr profile image
Vijayr

Informative. Glad to know. Thanks

Collapse
 
emlautarom1 profile image
Martín Emanuel

Thanks for the short explanation! I'm getting into CSS and this really comes in handy.

Collapse
 
flippedcoding profile image
Milecia

I'm glad it was helpful!

Collapse
 
44r0n profile image
Aarón

Thank you! You helped me a lot!