Here’s a summary of how to stop using px for font-size and why it’s recommended to switch to relative units like rem or em.
Why You Should Stop Using Pixels (px) for Font Sizes
- Accessibility:
- Pixels are absolute units and don’t respect browser settings for users who need larger text sizes.
- Many users with limited vision set larger base font sizes in their browsers, but pixel-based fonts won’t scale accordingly.
- Relative Units Are Better:
rem(root em) andemare relative units that adjust based on the root font size of the browser or parent element.- They allow the entire website to scale proportionally when users adjust their browser’s default font size.
How to Transition from Pixels (px) to Rems (rem) for Font Sizes
1. Understand rem Basics
- 1 rem equals the root font size (default is 16px in most browsers).
Example: 2rem=2 * 16px = 32px- If the browser’s base font size changes to 24px,
2rem=2 * 24px = 48px.
2. Convert Pixels to Rems
- Formula:
rem = px / 16
Example: - 16px =
1rem - 24px =
1.5rem - 32px =
2rem
3. Using CSS Custom Properties to Simplify Conversion
Define your pixel-to-rem conversions as CSS custom properties in the :root selector to avoid repeated calculations.
:root {
--fs-16px: 1rem; /* 16px */
--fs-24px: 1.5rem; /* 24px */
--fs-32px: 2rem; /* 32px */
}
Now, use these properties for font sizes:
h1 {
font-size: var(--fs-32px);
}
p {
font-size: var(--fs-16px);
}
4. Switching to SCSS (or Sass) Functions
If you use Sass, create a function to convert pixels to rems automatically:
@function rem($px) {
@return #{$px / 16}rem;
}
Usage example:
h1 {
font-size: rem(32); // Compiles to 2rem
}
p {
font-size: rem(16); // Compiles to 1rem
}
This saves time and avoids manual calculations.
5. Updating Media Queries with Relative Units
Instead of using px in media queries, switch to em or rem:
@media (max-width: 62.5em) { /* 1000px / 16 = 62.5em */
body {
display: block;
}
}
Why?
- Relative units ensure that media queries adapt if the user changes the base font size.
6. Avoid the 62.5% Font Size Hack
- In the past, developers used the 62.5% trick to set the
htmlelement’s font size to 10px (62.5% of 16px) to make calculations easier. - Problem: It breaks compatibility with third-party libraries that expect a 16px base font size.
7. Summary Workflow for Transitioning from Pixels to Rems
- Replace
pxvalues with calculatedremvalues (divide by 16). - Use CSS custom properties or Sass functions to simplify your workflow.
- Avoid using
pxin media queries; switch toemorrem. - Test your website with different browser font size settings to ensure accessibility.
Benefits of Using Rems Over Pixels
- Scales with User Preferences: Users can adjust browser font sizes, and your website will scale smoothly.
- Improved Accessibility: Easier to meet WCAG guidelines for text resizing.
- Future-Proof Layouts: Your layouts remain consistent even with third-party libraries.
By making these changes, your website will become more accessible, adaptable, and maintainable.

