· 10 min read

Why You Should Move from Pixels (px) to Relative Units (rem) in CSS

Switching from pixels to relative CSS units like rems improves accessibility, responsiveness, and maintenance of sites over time.

An in-depth guide on why you should move from using pixels (px) to relative units like rems for sizing elements in CSS. Details the benefits of fluid relative units for responsiveness, accessibility, and maintaining sites over time.

For years, pixels (px) have been the default unit of measurement for everything on the web. They offered an easy way to precisely size elements by specifying exact pixel values. However, as screen sizes, devices, and browsing methods have diversified, rigid pixels can cause major issues with responsive web design and accessibility.

That’s why relative units like rems are becoming increasingly popular and even recommended as a best practice in CSS. Rems can help create truly responsive, resilient websites that gracefully adapt to any screen size, device, or user preference.

In this in-depth guide, we’ll compare rems and pixels, and make the case for why shifting to rems can benefit your site. We’ll cover:

  • The key advantages of rems
  • Common issues with fixed pixel units
  • Tips for transitioning from px to rem
  • When you may still want to use pixels

By the end, you’ll understand the benefits of flexible relative units like rems and have actionable advice for starting to use them in your own CSS. Let’s dive in!

Key Advantages of Using Rems Over Pixels in CSS

Let’s start with a high level overview of some of the core benefits that switching from rigid pixels to fluid rems offers:

1. Rems Provide Major Accessibility Advantages

Accessibility should be at the forefront of every web designer’s mind. Using relative units like rems is one of the easiest ways to improve the accessibility of your site.

When text and elements are sized using relative units, they can scale up or down based on the user’s text size preferences. If a user has trouble reading smaller text, they can zoom in and the rem units will adjust in proportion.

However, if you size text and elements using fixed pixel values, the user may have trouble reading the content if they try to zoom or override the text size. The pixels stay the same size regardless of their settings.

By moving to rems, you ensure your content reflows responsively based on each user’s needs. Your site remains readable and accessible no matter a user’s vision or text size preferences.

2. Rems Make Responsive Design Much Easier

Responsive web design is an essential component of modern web development. Your site needs to adapt seamlessly to smaller phone screens, larger desktop monitors, and everything in between.

When screen sizes change, relative units like rems fluidly scale up or down in proportion to the user’s default font size. However, elements sized with fixed pixel widths can become too large and bulky or too tiny and illegible on different screen sizes.

Rems eliminate the need to write media queries to adjust font sizes and element dimensions for every possible device. Instead, proportions adapt naturally based on the user’s default text size for their screen.

Furthermore, rems make it easy to consistently scale your entire site up or down for accessibility. With one global change to html or body font-size, your complete layout changes proportionally.

3. Rems Help Avoid Compounding Sizing Issues

Here’s another advantage of rems: they avoid compounding size issues when you nest elements.

For example, let’s say you set a container div to have a width of 500px. Then, inside of it, you add a child div with a width of 300px.

If the parent container uses fixed pixels, the child element will compound that 500px width, making it very large on wider screens. On mobile, it could become tiny and squash the contents.

However, with rems, child elements inherit the root html font-size no matter how deep they are nested. This prevents layout issues caused by compounding fixed pixel sizes.

4. Rems Simplify Global Styling Changes

Making site-wide design changes is also streamlined with rems compared to pixels.

With fixed pixel units, if you wanted to globally increase or decrease the sizes of fonts, paddings, margins, etc., you’d have to manually change every instance across all files.

However, with relative rems, you can efficiently scale your entire site by simply adjusting the base font-size value on the html or body selector. This cascades proportionally across all elements, like increasing or decreasing zoom.

For example, to increase overall size, simply change:

html {
  font-size: 100%; 
}

to

html {
  font-size: 120%;
}

This seamlessly scales up the entire site by 20% with one css change, rather than manually updating potentially hundreds of pixel values.

5. Rem Proportions Align With Visual Design Principles

Relative rems also make sense from a visual design perspective. Most designers use principles like grids, columns, modular scales, and consistent spacing to lay out sites.

These techniques rely on elements having proportional sizes and spacing. For example, having a gutter that’s 1/3 the size of a column width.

Pixels don’t align with these proportional theories. But rems match the way designers already think about relative sizing and spacing when constructing layouts.

6. Browser Default Text Sizes Use Rems

Browser developers have moved toward using relative rem units internally for default text sizes.

For example, here are the base font size defaults for different browsers:

  • Chrome/Safari: font-size: 100% = 16px
  • Firefox: font-size: 100% = 16px
  • Edge: font-size: 100% = 14px
  • IE11: font-size: 100% = 14px

If you also define your own text sizes in rems, they’ll mesh cleanly with the browser’s default text size calculations.

Consistency between your units and the browser’s defaults leads to greater consistency in actual rendered sizes across browsers and devices.

On the other hand, inconsistencies between browser defaults and your own fixed pixels can cause visual bugs. Sticking with rems improves integration with each browser’s built-in text handling.

Common Issues Caused By Using Pixels in CSS

Beyond the advantages of rems, there are many issues that arise from overuse of fixed pixel units:

  • Difficulty Scaling Text and Layouts

    • As mentioned, using fixed pixels makes it tricky to uniformly scale up or down for accessibility or responsive design. You have to manually update pixel values across files anytime you want to globally scale.
  • Accessibility Problems

    • Users with visual impairments or who prefer larger text have trouble reading and navigating pixel-based sites as they can’t scale the text.
  • Cumbersome Media Queries

    • Each breakpoint in your CSS needs pixel-based media queries to adjust sizes for each viewport width. This bloats stylesheets and complicates responsiveness.
  • Compound Sizing Bugs

    • Nesting only works reliably with relative units. Pixels compound issues as described above when elements are nested.
  • Inconsistent Rendering Across Browsers

    • Varied browser default sizes means pixels render inconsistently across browsers. Rems avoid this by being relative to the user agent’s defaults.
  • Unnecessary Technical Debt

    • Overuse of pixels leads to accumulated responsive and accessibility bugs over time. They require constant updates and maintenance.

So in summary, fixed pixels insert a permanent structure that prevents seamlessly adapting to new contexts. Like any outdated technical choice, they incur technical debt and maintenance costs down the road.

Tips for Transitioning From Pixels to Rems

Hopefully you’re convinced of the benefits of switching from rigid pixels to flexible rems for most CSS properties. Here are some best practices for making the transition smoothly:

  1. Use Pixels for Media Assets

    One place where fixed pixels still make sense is sizing media assets like image widths. For example:

    img {
      width: 480px;
    }

    Just don’t use pixels for any containers, text, spacing, etc. around the media items.

  2. Set html or Body Font-Size in Rems

    Define your base font-size on html or body in rem units:

    html {
      font-size: 100%; /* 16px default */
    }

    Or even better, assign it to a variable:

    $base-font-size: 16px;
    
    html {
      font-size: $base-font-size; 
    }

    This allows easily scaling the entire site proportionally by changing one variable value.

  3. Convert Existing Pixels to Rems

    For any existing pixel values, convert them to rem units.

    The formula is:

    px value / base font size in px = rem value

    For example, if your base font size is 16px:

  • 32px -> 32px / 16px = 2rem
  • 24px -> 24px / 16px = 1.5rem

This ensures the sizes remain exactly the same during initial conversion.

  1. Use Relative Units Going Forward

    When adding new styles, use rem, em or % units. This avoids adding to your legacy pixel debt going forward.

  2. Use em for Nested Elements

    For nested elements, you can use em units to size in relation to the parent rather than being relative to the root.

    For example:

    .parent {
      font-size: 2rem;
    }
    
    .child {
      font-size: 1.5em; // 1.5 x parent size
    }

    This helps child elements scale proportionally to their container rather than the global root size.

  3. Don’t Convert Everything All at Once

    It’s fine to take an incremental approach. Convert global styles like fonts, buttons and spacing first. Then slowly refactor components to use relative units.

    Trying to convert a large legacy codebase all at once can cause bugs. Take it slow and focus on new code or key responsive areas first.

When You May Still Want to Use Pixels

While rems should be your new default in CSS, there are still a few cases where fixed pixel units may make more sense:

  • Media assets like image dimensions
  • Borders with exact pixel widths
  • Fixed navigation bars or headers
  • Context where exact pixel precision is required

The key is being judicious with pixels rather than defaulting to them for everything. Use them strategically in limited cases, and rely on relative units like rems for most styling needs.

Think of pixels like salt. A pinch can enhance flavor but dumping spoonfuls ruins the whole dish. Season judiciously.

Summary and Key Takeaways

The web has evolved rapidly, but many sites still limit themselves to outdated practices like overuse of fixed pixel units. Keeping up with modern best practices like switching to relative CSS units helps ensure your site remains responsive, accessible, resilient and easier to maintain over time.

Let’s recap the key benefits of moving from pixels to rems:

  • Rems fluidly scale in proportion instead of being fixed units
  • They improve accessibility by allowing users to scale text
  • Rems simplify responsive design without complex media queries
  • Global styling changes are easy with rems on html or body
  • Rems align with proportional design principles
  • Browser default text also uses rem, improving consistency

By thoughtfully transitioning to rems for most CSS properties, you’ll future-proof your site and eliminate potential responsive and accessibility bugs before they happen.

Take it incrementally, and focus on migrating global styles and components with key responsive needs first. Use pixels judiciously for assets, borders or fixed headers/footers as needed.

Adopting rems does take some adjustment from old habits. But spending time mastering relative units pays off tremendously in saved development and maintenance costs over the life of a site.

By keeping up with modern best practices like rems, we can build sites that stand the test of time and provide optimal experiences for all users. The web is always changing, but robust relative units like rems give your CSS the flexibility to smoothly adapt and endure.

Need Help Transitioning to Relative Units?

If you’re convinced of the benefits of using relative units like rems in your CSS but need assistance in implementing this change across your website or web application, I offer freelance services to help you make the transition smoothly.

With my expertise in responsive web design, accessibility best practices, and CSS architecture, I can ensure that your codebase is optimized for maintainability and future-proofed for evolving device sizes and user preferences.

Whether you’re working on a new project or looking to refactor an existing codebase, I can work with you to analyze your specific requirements and deliver a tailored solution that aligns with your goals and design system.

Feel free to reach out to me here - Contact Seerat Awan to discuss your project and get a quote.

Seerat Awan

Tech Enthusiast | Frontend Developer

@seeratawan01

Subscribe to my newsletter to get the latest updates on my blog.

Back to Blog