CSS Unit: em

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!
No comments yet. Be the first to comment.
This series is my log of what I learned about responsiveness in CSS
Hello Fellow Codenewbies ๐ You probably use px or em to set up the size of your image in CSS. Well, I did. But there is a good practice that I learned recently on how to set up the size of images, particularly the width. Percentage is better to be ...
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...

On this page
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?
Let's create:
h1 and a p tag inside a container.font-size of the h1 to 3remmargin-bottom of 1em to the h1.We will not set anything to the p tag because we only want to focus on the margin-bottom where we apply the em unit.
Click on the CSS button on Codepen to see the code.
Now change the font-size to 5rem.
h1 {
font-size: 5rem;
margin-bottom: 1em;
}
When you see the rendered page, do you notice that the size of margin-bottom is the same as the size of h1's font-size?
But we've set the margin-bottom to 1em. So the size should be 16px since we don't declare font-size anywhere except the h1, right?
Beside inheriting size from its parents, another thing to know about em is that it is relative to the font-size of its element.
Say we set a font-size for an element. And then set the value of margin or padding for the same element in em. This margin or padding will be relative to the font-size of the element.
When we do responsiveness, the use of em in margin or padding would be convenient.
That's because we don't need to change the size of the margin or padding whenever we change the font-size.
Thank you for reading!
Last, you can find me on Twitter. Let's connect! ๐