CSS Units

Ayu is a tech blogger, open source contributor & maintainer, and tech community enthusiast based in the Netherlands. She loves photography and iced coffee!
Search for a command to run...

Ayu is a tech blogger, open source contributor & maintainer, and tech community enthusiast based in the Netherlands. She loves photography and iced coffee!
Most of the time, we know how wide the margin or padding between elements that we want.
When we use em, we don't need to adjust the margin and padding manually whenever we change the font size because it will follow the font size.
For example:
If the font size is 16px and we set the margin to be 2em, then the margin will be 32px. And if we change the font size to 12px, then the margin will become 24px.
I hope this makes sense ๐
This series is my log of what I learned about responsiveness in CSS
Hello Fellow Codenewbies ๐, If you've read my previous post, you've also learned that em can cause problems since it creates cascading effect. That's why we preferably avoid using it for font-size. So when or where can we use em? An Example Let's c...
Hi friends ๐, I've been writing tech blog posts for a few years now. If you don't know me yet, English is my second language. My mother tongue is Indonesian, and I'm also surrounded by Dutch every da

I frequently receive messages from enthusiastic contributors who have completed 2 or 3 high-quality pull requests (PRs) in just 1 month. Theyโre proactive, their work is top-tier, and theyโve handled

A few months ago, I shared a story about building an automated open source portfolio using just my smartphone and an AI assistant while on vacation. My main goal was to stop the "spreadsheet struggle"

Recently, I read Bekah Hawrot Weigelโs blog post, "When 'Local' Went Global: The Pandemic Era of International Communities." As someone active in many online communities, I found it a bit sad, but I agree with her points and think they apply to open ...

Hacktoberfest is almost over. How has your contribution journey been so far? Are you confident about contributing to open source projects, or are you still struggling to find your way? If you're new to open source and still feel overwhelmed, that's c...

Hello Fellow Codenewbies ๐,
There are many factors to consider when we think about responsiveness in CSS.
One of them is CSS Units.
There are two types of units:
Regardless of its parent's or screen size, absolute units are always the same size.
px, cm, inch, etc. are within this category.
<body>
<div class="absolute"></div>
<div class="relative"></div>
</body>
.absolute {
background-color: green;
width: 640px;
height: 100px;
}
.relative {
background-color: red;
width: 90%;
height: 6em;
}
Try to shrink and expand the window and see the difference in the above example.
The percentage is mainly used for widths, and it is relative to the target element's parent.
<body>
<header>
<h1>Hello World</h1>
</header>
</body>
header {
width: 80%;
}
h1 {
width: 50%;
}
In the above example, the width of the header is 80% of the body.
And the width of h1 is 50% of header.
em inherits size from its parent.
<div class="container">
<h1>Hello World</h1>
</div>
.container {
font-size: 12px;
}
h1 {
font-size: 2em;
}
The font size of h1is 2x its parent (container), making it 24px.
But there is one problem with em.
Because it is relative to its parent, it can create a cascading effect.
Click the CSS button on the CodePen to see the code.
As you can see in the result, the fonts of li are bigger than h1. But their font size is smaller than h1.
How does this happen?
Let's break them down.
body as the parent of h1 and ul to 18px.h1 to 2em. It now makes the font size of h1 twice bigger than body.h1 and move to its sibling, ul. We set its font size to 1.5em. As you can guess, the size of the ul is now 1.5x bigger the parent.li to 1.5em. It makes li now 1.5x bigger than ul.
Remember that ul is already 1.5x bigger than body. So, li is 3x bigger than its grandparent, the body.When it's about font-size, we better avoid the use of em and use rem instead.
rem stands for "root element," and it is always relative to the "root" of the document, which is the html element.
Because we use rem, even though the font size of body is 50px, all font size is changed relative to html as the root element.
It depends on your personal preference and what you need, but here is a reference of approaches on when to use one:
rememem or percentage or absolute unit such as pxThank you for reading!
Last, you can find me on Twitter. Let's connect! ๐