An EM cheat sheet for coding is a quick reference for the CSS em unit, which sets font sizes and spacing relative to the parent element’s font size. The key rule: 1em equals the current font size of the parent, so 2em is double that size. This makes em scalable and responsive, but it also means values compound across nested elements.
What EM Means in CSS and How It Behaves
The em unit is a relative length in CSS. It is calculated from the computed font-size of the element’s parent. For example, if a parent has font-size: 16px, then 1em on a child is 16px, 1.5em is 24px, and 2em is 32px.
When you use em for properties other than font-size (like padding, margin, or width), the value is still based on the element’s own font-size, not the parent’s. This distinction is crucial: font-size in em inherits from the parent, but other properties in em refer to the element’s own computed font-size.
EM vs. REM vs. PX: Which to Use When
| Unit | Relative to | Best for |
|---|---|---|
| px | Absolute (1px = 1/96 inch) | Fine borders, shadows, or when you need exact size |
| em | Parent font-size (for font-size) or own font-size (for other properties) | Scaling components that should resize together |
| rem | Root (html) font-size | Global spacing and typography that should stay consistent |
A simple rule: use rem for most spacing and font sizes to avoid compounding surprises; use em when you want a component to scale proportionally with its own font size, like a button that grows when the text grows.
How to Convert Between EM and PX
To convert em to pixels, multiply the em value by the parent font-size (for font-size) or the element’s own font-size (for other properties). The formula: px = em × base-font-size.
For example, if the base font-size is 16px, then 1.25em = 20px, and 0.875em = 14px. To convert from px to em, divide the pixel value by the base font-size: em = px / base-font-size.
Remember: the base font-size changes with nesting. If a parent has font-size: 20px and a child has font-size: 0.5em, the child’s computed size is 10px (20 × 0.5). Then if the child has padding: 1em, that padding is 10px (based on the child’s own font-size).
Common EM Values and Their Pixel Equivalents
Here is a quick reference for common em values when the parent font-size is 16px (the browser default):
| em | px | Use case |
|---|---|---|
| 0.5em | 8px | Small spacing or captions |
| 0.75em | 12px | Footnote text |
| 1em | 16px | Body text |
| 1.25em | 20px | Subheadings |
| 1.5em | 24px | Section headings |
| 2em | 32px | Page titles |
If the parent font-size is different, adjust accordingly. For instance, with a parent of 18px, 1.5em becomes 27px.
Practical Example: Building a Scalable Button
Suppose you want a button that scales with its own font size. Set the button’s font-size in em relative to its container, then use em for padding and border-radius so everything scales together.
.container {
font-size: 16px;
}
.button {
font-size: 1em; /* 16px */
padding: 0.5em 1em; /* 8px 16px */
border-radius: 0.25em; /* 4px */
}
.button--large {
font-size: 1.25em; /* 20px */
/* padding and radius automatically scale to 10px 20px and 5px */
}
Now if you change the container’s font-size to 20px, the button’s font-size becomes 20px, and its padding and radius scale proportionally. This keeps the button visually balanced without rewriting values.
Watch Out for Compounding EM in Nested Elements
The biggest pitfall with em is compounding. If you have nested elements each using em for font-size, the size multiplies at each level. For example:
div { font-size: 1.2em; } /* 16px × 1.2 = 19.2px */
div div { font-size: 1.2em; } /* 19.2px × 1.2 = 23.04px */
div div div { font-size: 1.2em; } /* 23.04px × 1.2 = 27.65px */
This can quickly make text larger than intended. To avoid this, use rem for font sizes that should not compound, or set explicit pixel values at key breakpoints. If you must use em, test at multiple nesting levels.
Quick Reference Card for EM in Coding
- 1em = parent font-size (for font-size) or own font-size (for other properties).
- Default browser font-size is usually 16px, but users can change it;
emrespects that. - Use
emfor components that should scale together (buttons, badges). - Use
remfor global spacing and typography to avoid compounding. - Use
pxfor fine details like 1px borders or shadows. - Check computed values in browser dev tools (right-click > Inspect > Computed) to see the actual pixel size.
Keep this cheat sheet handy: the core idea is that em is always relative to a font size, and that reference changes depending on the property and the element. With practice, you will know when to reach for em and when to choose rem.
Related
- Chess for Beginners Cheat Sheet: Rules and Moves
- Bullet Journal Color Coding for Beginners
- Time Blocking Color Coding: How to Start
General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more
Spotted an error? Tell us and we will correct it (corrections policy).