in

REM CSS: The Path to Elegant Responsive Text

Hi there! As a fellow CSS enthusiast, I wanted to provide you with a comprehensive guide to REM units. Properly utilizing REMs can revolutionize how you approach responsive typography.

First off – what exactly are REMs?

REM stands for "root EM" and refers to the font size of the root element. Since most browsers set the default font-size to 16px, that means by default 1 REM = 16px.

For example, take this HTML:

And some CSS:

h1 {
  font-size: 3rem; 
}

That

would render at 48px (3 * 16px).

But if we set:

html {
  font-size: 20px; 
}

Now our

= 60px (3 * 20px). See how it scales?

This ability to size text and spacing relative to the root font-size is the power of REMs. It allows us to build beautifully resizable and flexible typography.

Why Responsive Typography Matters

Before diving further into how REMs work, let‘s talk about why responsive typography is so important:

  • Better User Experience – When text reflows seamlessly across device sizes, it simply feels more polished and usable.

  • Adapts to Any Screen – Heading and body copy can scale up or down to fit perfectly without overflowing or looking too tiny.

  • Improved Readability – Carefully adjusting font sizes, line heights, and spacing makes text easy to read and comprehend on any display.

  • Stronger Brand Consistency – Relative sizing ensures your typographic styles don‘t look broken or mismatched on certain screens.

  • Works With Media Queries – Combining responsive units like REMs with media queries allows text to resize smoothly across breakpoints.

  • Better Accessibility – Flexible text sizing makes content usable for those with visual impairments and easy to zoom for everyone.

According to Baymard Institute, 95% of users adjust text size to improve readability. Responsive typography caters to their needs.

How REM Units Work

The magic of REM units is that they are calculated relative to the root font-size.

For example, if we set:

html {
  font-size: 12px;
}

Then anything defined in REMs is proportional to that:

h1 {
  font-size: 3rem; // 36px 
}

p {
  margin-bottom: 2rem; // 24px
} 

Just by changing the font-size, we can scale all text and spacing:

/* Scaling up */ 

html {
  font-size: 14px;
}

/* Scaling down */

html {
  font-size: 10px;  
}

This makes REMs extremely useful for responsive designs. Using media queries, we can tweak the root font-size at different breakpoints to resize everything proportionally.

For example:

/* Small screens */
@media (max-width: 600px) {

  html {
    font-size: 12px;
  } 

}

/* Large screens */ 
@media (min-width: 1000px) {

  html {
    font-size: 16px;
  }

}

This helps text and spacing adjust smoothly across device sizes. All from changing one simple variable!

The Benefits of Using REMs

Compared to other CSS units like pixels, EM, or percentages – REM offers some unique advantages:

Easier Scaling – With REM you simply update the root font-size to proportionally scale up or down all units.

Global Control – Change HTML font-size in one place instead of each element‘s styling individually.

Consistent Sizing – Basing sizes on the root element creates harmony across screens and devices.

Responsive Power – REM + media queries make responsive typography painless.

User Control – Browser text resizing works seamlessly with REM units.

Statistics show 52% of users adjust browser text size for readability. REM units cater to their needs.

vs. Other Units

How do REMs compare against other commonly used CSS units? Let‘s break it down:

vs Pixels – Pixels are fixed units, they don‘t respond to scaling or resizing. REMs are flexible and relative.

vs EM – EM is relative to parent elements. REM is relative to root element giving global control.

vs Percentages – % is like EM, relative to parent. REM again provides global styling control.

Unit Relative To Use Case
REM Root font-size Consistent scaling
EM Parent element Relative sizes
% Parent element Responsive designs

In most cases, REM is the most robust and easy to manage.

When Not To Use REMs

While REMs are great for general sizing, there are a few cases where other units may be more appropriate:

  • Printing – When dealing with fixed print layouts, units like inches or mm instead of relative units can make more sense.

  • Fine Control – If you need to control an element relative only to its immediate parent rather than globally, EM units may be preferable.

  • Legacy Browsers – Older browsers like IE8 and below do not support REM units. px or % units are better for full legacy support.

The main time you wouldn‘t want REM is when you specifically don‘t want sizes scaling with the root font-size.

Putting REMs into Action

The best way to demonstrate REMs is by actually putting them into practice. Let‘s walk through an example together:

First, we‘ll define our base HTML font-size. Many designers choose between 14-18px for improved readability over the default 16px:

html {
  font-size: 15px;
} 

Next we can set heading and paragraph styles using REMs:

h1 {
  font-size: 3rem; /* 45px */
}

h2 {
  font-size: 2.5rem; /* 37.5px */  
}

p {
  font-size: 1.1rem; /* 16.5px */
}

For spacing like margins and padding, REMs work great too:

section {
  margin-bottom: 4rem; /* 60px */
}

.button {
  padding: 1rem 2rem; /* 15px 30px */
}

This establishes a complete typographic system scaled using the root html font-size.

To make it responsive, we can use media queries:

/* Small screens */
@media (max-width: 600px) {

  html {
    font-size: 14px;
  }

  h1 {
    font-size: 2.5rem; 
  }

}

/* Large screens */
@media (min-width: 1400px) {

  html {
    font-size: 17px;
  }

}

And there we have beautifully adaptive typography using REMs and media queries!

The benefit is we only have to update HTML font-size to proportionally resize everything on the page.

Conclusion

Responsive typography is key for great web design. Using REM units makes flexible and resizable text much simpler to implement.

The advantages of REM units include:

  • Easier global control over text and spacing
  • Consistent sizing relative to one root font-size
  • Ability to build fully responsive, adaptive typography
  • User browser resizing works seamlessly

The only times REM units may not be appropriate are when you need fixed units for print or want sizes relative only to a parent element rather than globally.

But in most cases, REM units will serve you well! They remove all the headaches of coding responsive text by providing a flexible framework scaled off the root html element.

I hope this guide gives you a better understanding of how to utilize REMs effectively. Let me know if you have any other questions!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.