Table of Contents
This is where ‘rem’ and ’em’ come in handy. They’re flexible and adjust to different screen sizes. Many developers need clarification on these units, though. In this guide, we’ll clear up the ‘rem’ vs ’em’ debate. You’ll learn how to use them to make websites that work well on all devices.
CSS Units: The Building Blocks of Web Design
CSS units are vital for web design. They set the size and place of every part of your page. Think of them as digital rulers, making sure your headings, text, images, and buttons fit together well. CSS units come in different types, each with its own uses.
The main types of CSS units are fixed and flexible.
- Fixed units, like pixels (px), don’t change size. One pixel is always one dot on your screen. This makes them easy to use, but they can need fixing with responsive design. Websites need to look good on both big and small screens. Using only fixed units is like building a house with stiff walls – it won’t handle changes well.
- Flexible units change size based on other factors, like screen size. They stretch and shrink to fit different devices, making them great for responsive design.
There are also 4 CSS units to choose from:
- Pixels (px): Fixed size, good for exact control
- Percentages (%): Flexible, often used for widths and heights
- Viewport units (vw, vh): Size based on the browser window
- ‘rem’ and ’em’: Flexible units we’ll focus on in this guide
Picking the right CSS unit is about more than just looks. It affects how easy your website is to use and update. Imagine text that’s too small to read on a phone or a layout that’s messy on a tablet. These issues can drive users away. By knowing the pros and cons of each unit, you can make websites that work well for everyone.
What is ‘rem.‘
rem’ is a flexible CSS unit that many web developers like. Let’s look at how it works and why it’s useful.
‘rem’ stands for “root em.” The font size is determined by the root element of your webpage, which is usually the \<html\> element. Here’s how it goes:
- If the root font size is 16px (common in most browsers), 1rem equals 16px
- If you change the root font size to 20px, 1rem would then equal 20px
This link between ‘rem’ and the root font size makes it great for scalable designs.
Think of your webpage as a building. The root element is the foundation, and all other elements are blocks on top. When you use ‘rem’ to size something, you’re saying, “Make this block X times bigger or smaller than the foundation.”
For example:
- Set root font size to 16px
- Make a heading 2rem
- The heading will be 32px (2 * 16px)
- If you change the root to 20px, the heading becomes 40px (2 * 20px)
This scaling is great for responsive design. By changing the root font size for different screen sizes, you can easily adjust your whole layout.
Benefits of Using ‘rem’
Using ‘rem’ has several perks:
- Easy Scaling: Want to change all font sizes at once? Just update the root font size. Everything using ‘rem’ will scale to match.
- Simpler to Maintain: ‘rem’ makes your CSS code cleaner and easier to understand. This helps when working with others or coming back to your project later.
- More Predictable: Unlike ’em,’ which is based on its parent’s font size, ‘rem’ always refers to the root. This makes it easier to guess how things will look.
When to Use ‘rem’
‘rem’ is handy in many situations:
- Font Sizes
Great for making text that’s easy to read on any device. This setup lets users zoom in or out, and the text sizes stay in proportion. For example:
css
html {
font-size: 16px; /* Base size */
}
h1 {
font-size: 2rem; /* 32px */
}
p {
font-size: 1.2rem; /* 19.2px */
}
- Element Sizing
Use ‘rem’ for widths, heights, margins, and padding. This keeps your layout balanced as screen sizes change.
- Overall Layout
Use ‘rem’ to set container widths and spaces between sections. This helps your whole page adjust smoothly to different screens.
By using ‘rem,’ you can build websites that look good and work well on all kinds of devices.
What is ‘em.’
While ‘rem’ provides a solid base for scalable web design, ’em’ offers a different way to size elements based on their surroundings. Let’s examine how ’em’ works and where it shines.
’em’ gets its name from the width of the letter ‘M’ in old-school printing. Unlike ‘rem’, which always looks at the root element’s font size, ’em’ takes its size from its parent element. This means:
- When a parent element’s font size is 16 pixels, its child’s 1em will also be 16 pixels.
- If the parent’s font size changes, the child’s ’em’ values will change too.
This scaling method makes ’em’ great for creating parts of a webpage that adjust to fit wherever you put them.
Think of ’em’ like a family tree of sizes. Each element passes its font size to its children, who then use that size to figure out their own ’em’ measurements. This can cause a domino effect, where changing one element’s font size affects all its descendants.
Let’s look at an example:
css
.container {
font-size: 16px;
}
.container h2 {
font-size: 2em; /* 32px */
}
.container p {
font-size: 1.2em; /* 19.2px */
}
In this case:
- The <h2> and <p> elements get their size from the .container.
- The <h2> will be twice as big as the container’s font size (32px).
- The <p> will be 1.2 times bigger (19.2px).
If we change the .container‘s font size to 20px, the <h2> and <p> will automatically adjust to 40px and 24px.
This behavior can be both helpful and tricky. It allows for flexible design, but it can also lead to surprises if you need to be more careful, especially in complex layouts.
Benefits of Using ’em’
- Adjusts to Its Surroundings
’em’ shines in its ability to change size based on its parent element. This is super useful when you want to create parts of your website that can fit in different places, each with its own font size.
For example, imagine a button you want to use in both your main menu and your sidebar. The menu might have bigger text than the sidebar, so you want the button to change size to match. By using ’em’ for the button’s padding, font size, and other measurements, you make sure it looks good no matter where you put it.
- Great for Building Blocks
’em’ works well with modular design, where you break your website into reusable parts. When you size elements within a component using ’em,’ you create a self-contained unit that scales based on its own font size. This makes it easy to use the component in different parts of your website without worrying about it looking out of place.
It’s like building with blocks. Each block has its own size, but they all fit together because they’re based on the same measurement. Similarly, ’em’ lets you create CSS “blocks” that keep their internal balance while fitting into the bigger picture of your website.
This approach not only makes your code more reusable and easier to maintain but also helps you work more efficiently. You can focus on getting each component just right, knowing they’ll fit in smoothly with the rest of your design.
Where to Use ’em’
’em’ is particularly useful in situations where you need flexibility within specific parts of your website.
- Keeping Things in Proportion
One of ’em’s main strengths is helping elements within a component stay in proportion to each other. This means that parts of a button or a menu can grow or shrink together when the component’s font size changes. This keeps everything looking balanced, no matter where you use the component on your site.
Imagine a button with padding and font size set in ’em.’ If you put this button in a container with larger text, the button will grow automatically, keeping its shape and look. This saves you from having to adjust the button’s style every time you use it in a new place.
- Creating Harmony Within Components
’em’ is also great at making sure different parts of a component look good together. By using ’em’ for things like margins, padding, and even image sizes, you can make sure they all change size together when the component’s font size changes. This creates a design where all parts of the component feel connected and balanced.
For example, think about a card component with a title, an image, and some text. If you set the image height and the spaces around elements using ’em,’ they’ll all grow or shrink together when the card’s font size changes. This keeps everything looking consistent and prevents any part from seeming too big or too small.
Let’s look at a practical example:
css
.card {
font-size: 16px;
}
.card h3 {
font-size: 1.5em; /* 24px */
margin-bottom: 0.5em; /* 8px */
}
.card img {
height: 10em; /* 160px */
margin-bottom: 1em; /* 16px */
}
.card p {
font-size: 1em; /* 16px */
line-height: 1.5em; /* 24px */
}
In this code, we’ve created a card component with a heading, an image, and a paragraph. All the sizes and spaces within the card are set using ’em,’ so they’ll change together if the card’s font size changes. This creates a component that looks good and can adapt to different uses across your website.
Potential Challenges with ’em’
While ’em’ offers flexibility, it’s important to be aware of some challenges to avoid surprises in your layouts.
1. Inheritance Can Get Complicated
Because ’em’ is based on inheritance, it can sometimes lead to unexpected results, especially in complex HTML structures. As each element takes its font size from its parent, changes made higher up can have surprising effects further down the road.
For example, if you have a list inside a container that uses ’em’ for its font size, changing the container’s font size will also change the list items’ sizes. This could make them too big or too small, causing layout problems. Fixing layout issues can be more complex, as you need to pay attention to how sizes are passed down through your HTML.
2. Testing is Key
Because of how ’em’ works, it’s really important to test your layouts thoroughly. You need to make sure everything looks good in different situations and when font sizes change.
Make sure to check your website on different devices and with different browser zoom levels to catch any layout issues. Browser developer tools can be really helpful for looking at inheritance chains and finding the source of any ’em’-related problems. By being careful and proactive in your testing, you can avoid frustrating surprises and create a polished, user-friendly website.
‘rem’ vs. ’em’ Comparison
Now that we’ve explored the individual strengths and quirks of ‘rem’ and ’em’, let’s put them side-by-side in a head-to-head comparison. This will give you a better idea of how they’re different and when you should use each one.
Side-by-Side Comparison Table
Feature
rem
em
Definition
Relative to the root element’s font size
Relative to the parent element’s font size
Inheritance
Does not inherit
Inherits from the parent element
Scalability
Excellent for overall website scaling
Good for scaling within components
Maintainability
Promotes cleaner, more maintainable code
This can lead to complex inheritance chains
Predictability
High predictability in element sizing
It can be unpredictable in nested structures
Ideal Use Cases
Font sizing, element sizing, general layout
Modular scaling, relative sizing within components
Advantages
Scalability, maintainability, predictability
Contextual scaling, flexibility within components
Disadvantages
Potential issues with nested elements, less control over fine-grained adjustments
Inheritance complexities, potential for unexpected behavior
Picking the Right CSS Unit: What to Think About
When you’re building a website, choosing the right CSS unit is important. Here’s what you should consider:
1. How Big is Your Project?
The size of your project matters:
- Big websites: ‘m’ might work better. It’s easier to scale and keep tidy.
- Small projects or reusable parts: ’em’ could be a good fit. It’s more flexible.
- What’s Your Design Goal?
Think about what you want to achieve:
- The whole website looks good together: ‘m ‘is better.
- Parts that can fit anywhere: ’em’ could be the way to go.
3. Is Your Site Easy for Everyone to Use?
Making your site work for all users is key:
- ‘rem’ is linked to the main font size. This can make it easier for users to change text size without messing up the layout.
- ’em’ can work, too, but you need to be careful about how sizes change within parts of your site.
4. Does Your Site Work on All Devices?
Both ‘rem’ and ’em’ can work well for sites that look good on all screens. But:
- ‘rem’ is more predictable. It always relates to the main font size.
- ’em’ can sometimes surprise you, especially in complex layouts. You’ll need to test it more.
Using ‘rem’ and ’em’ Together: Best Ideas
You don’t have to pick just one. Using both ‘rem’ and ’em’ can make your site even better. Here’s how:
- Start with ‘rem’ for the basics: Set your main font size with ‘rem’. This sets the stage for your whole site.
- Use ’em’ for parts that need to fit anywhere: For things like buttons or menus, ’em’ helps them fit in wherever you put them.
- Stick to ‘rem’ for the big stuff: Use ‘rem’ for the main layout, like how wide things are or how much space is between sections. It keeps things consistent.
- Mix it up for nested parts: If you have things inside other things, try using both. Use ’em’ for sizes within a component and ‘rem’ for anything that needs to match the overall layout.
- Try different things: Be bold and experiment. Test different mixes of ‘rem’ and ’em’ to see what works best for your site.
By using ‘rem’ and ’em’ together, you can make sites that look good and work well on all devices.
Cool Tricks and Tools
As you get better with ‘rem’ and ’em,’ you can use some advanced techniques:
1. CSS Variables: Your Design Control Center
CSS variables let you store values you use often. This makes changing your design much easier.
Here’s how it works:
css
:root {
--main-font-size: 16px;
}
body {
font-size: var(--main-font-size);
}
h1 {
font-size: calc(2 * var(--main-font-size));
}
In this example, we set a main font size and use it for the body text and to calculate the heading size. If we want to change the size later, we just change the variable once, and everything updates!
2. The calc() Function: Do Math in Your CSS
The calc() function lets you do math right in your CSS. This gives you more control over your layout.
For example, you could set a sidebar width like this:
css
.sidebar {
width: calc(20vw - 20px);
}
This makes the sidebar 20% of the screen width minus 20 pixels. It adjusts automatically for different screen sizes.
3. Elementor: Making CSS Units Easy
If you want to build a website without writing CSS code directly, try Elementor. It’s a visual builder that lets you:
- Change sizes and styles with intuitive controls
- See how your site looks on different devices
- Set styles for your whole site in one place
Elementor handles the calculations for relative units like rem and em so that you can focus on the design.
Common Mistakes and How to Fix Them
Even if you know a lot about ‘rem’ and ’em,’ you might still run into problems. Here are some common mix-ups and how to avoid them:
Myth 1: ’em’ is Always Better for Accessibility
Truth: Both ’em’ and ‘rem’ can work well for making sites easy to use. ‘m’ can be better for letting users change the overall text size.
Myth 2: ‘rem’ is Only for Font Sizes
Truth: You can use ‘rem’ for any size on your page – width, height, margins, and more.
Myth 3: ’em’ is Always Unpredictable
Truth: With good planning and testing, ’em’ can be reliable and flexible.
Myth 4: You Should Always Choose One Over the Other
Truth: Often, using both ‘rem’ and ’em’ together works best.
Tips for Fixing Problems
If you run into issues with ‘rem’ or ’em,’ try these tips:
- Check the size chain: Use your browser’s tools to see which elements are affecting sizes.
- Look for conflicting styles: Sometimes, other CSS can overrule your ‘rem’ or ’em’ settings.
- Test on different devices: Always check how your site looks on phones, tablets, and computers.
- Use CSS variables and calc(): These tools can make your CSS more flexible and easier to manage.
- Try a visual builder: Tools like Elementor can help you manage sizes without getting lost in code.
Wrapping Up
Both ‘rem’ and ’em’ are useful tools for web designers. ‘rem’ is great for keeping your whole site consistent, while ’em’ helps make flexible components.
There’s no one right answer—the best choice depends on what you’re building. Understanding both units helps you make smart decisions about your websites.
Don’t be afraid to mix ‘rem’ and ’em’ to get the best results. And if you want an easier way to design, check out tools like Elementor.
Remember, practice makes perfect. The more you work with these units, the better you’ll get at creating great websites. Happy designing!
Looking for fresh content?
By entering your email, you agree to receive Elementor emails, including marketing emails,
and agree to our Terms & Conditions and Privacy Policy.