REM Unit In CSS

Posted on • Updated on

Understanding REM Unit In CSS

#css#html#webdev#frontend

In this article we will discuss the purpose, advantages and use cases of REM (Root EM) units in CSS layout.


What is rem unit?


rem stands for root em, which is a relative measurement unit that refers to the font-size of the root element of a document.


It is a relative unit, which means all values that use it change when the root's font-size changes. The root element refers to the html element.


1rem means 1 times the root font-size.


if you declare the root's font-size to be 16px (which is the default font size)


html {
  font-size: 16px;
}

then 1rem will interpret it as 16px, 2rem will interpret it as 32px, and so on.


Using rem (or another relative length value) for font-size is a must for accessibility, because px in some browsers doesn't resize when the browser settings are changed.


Some users, for example, need to zoom maybe up to 400% to be able to read your text, due to a visual impairment. Using rem you make sure that your text respects these needs, because the font-size is defined relative to the default font-size a user has chosen.


Where to use rem unit?


rem unit provides flexibility in our design. It provides a great opportunity to change the whole site's typography and spacing up and down just by changing the font size in one place in a root element.


We can use this flexibility to make our designs easier to adjust during development, more responsive, and allow browser users to control the overall scale of sites for maximum readability.


So, we conclude that rem is used where we want to make changes in a whole site because it allows us to only change the font size in a single place.


Relative Units vs Absolute Units in CSS


We have mentioned that rem unit is a relative unit, so lets deep dive into CSS unit types, to understand what relative and absolute types mean.


Relative units


Relative units are defined in terms of some other value. These are, for example, REM, EM, VW and VH


REM is defined relative to:


  • the font-size of the root(html) element of the webpage
  • the font-size of the user's browser appearance settings(by default 16px) if it is not provided in the root element of the webpage

EM is defined relative to:


  • the font size of the parent element when the property font-size is concerned.
  • the font size of the element itself when we're dealing with other properties like height.

VW stands for 1% viewport width. That is to say that if you define the width property as 10vw, the element will take up 10% of the available viewport's width.


These relative length values have a clear advantage above the absolute ones, because they can help make your website responsive. That is to say, your website automatically adapts to the size of the available screen size, doing so in a predictable way.


Absolute units


Examples of absolute length values are: px (which is 1/96th of an inch), in (an inch) or cm (which is 37.8px or 25.2/64in).


When you use these units you can be sure that they will always be the same size. This is especially useful when you know the exact dimensions of the output, which is mostly with print layouts. But it's not so useful when this is not the case, like with all the different screen sizes out there.


REM accessibility


Google developers make the recommendation to use relative units for font sizes


By using rem you respect the users' decisions about the way they want to browse the web.


REM key points


  • rem units are computed into pixel values by the browser, based on font sizes in your design.
  • rem units are based on the font size of the HTML element.
  • rem units can be influenced by font size inheritance from browser font settings.
  • Use rem units that should scale depending on browser font size settings.
  • Use rem units on media queries.

REM conversion tables


The rem size is 16px in the following examples


rem converted to different units:


1rem 10rem 50rem 100rem
px 16px 160px 800px 1600px
pt 12pt 120pt 600pt 1200pt
in 0.167in 1.667in 8.333in 16.667in
pc 1pc 10pc 50pc 100pc
cm 0.423cm 4.233cm 21.167cm 42.333cm
mm 4.233mm 42.333mm 211.667mm 423.333mm

Different units converted to rem:


1 10 50 100
px 0.063rem 0.625rem 3.125rem 6.25rem
pt 0.083rem 0.833rem 4.167rem 8.333rem
in 6rem 60rem 300rem 600rem
pc 1rem 10rem 50rem 100rem
cm 2.362rem 23.622rem 118.11rem 236.22rem
mm 0.236rem 2.362rem 11.811rem 23.622rem

Conclusion


rem is a relative unit that allows you to define a global font-size, and every other value with the rem unit depends on the global size. If the user changes their default root font size, the rem unit values will also scale accordingly. In the case of fixed values, the user's preference will be ignored.

FAQs About CSS Rem Unit

We'll end by answering some of the most frequently asked questions about CSS rem unit.

What is the difference between rem and em units?

The main difference between rem and em units lies in their reference points for base size. The em unit is relative to the font-size of its parent element. rem stands for "root em", it's only relative to the root element (html). It makes rem unit more predictable and easier to manage in large websites.

How to convert pixels to rem unit in CSS?

To convert pixels to rem, you need to know total size of 1rem, default is 16px
Then, just apply formula: px / rem
For example, with 16px size of 1rem, 32px will be converted to: 32 / 16 = 2rem
Check out our PX to REM converter which allows you to easily convert it.

How to convert rem unit to pixels in CSS?

To convert rem to pixels, you need to know total size of 1rem, default is 16px
Then, just apply formula: rem * rem size
For example, with 16px size of 1rem, 2rem will be converted to: 2 * 16 = 32px
Check out our REM to PX converter which allows you to easily convert it.

Can I use rem units for styles other than font-size?

Yes, rem unit can be used for every CSS style property that accepts a length value, not just font-size. For example: width, height, padding, margin, and line-height. Using rem unit for these kind of style properties can help maintain proportional layout for different screens sizes.

Is rem unit supported for all browsers?

Yes, rem unit is widely supported in modern browsers, including Chrome, Firefox, Safari, and Edge. You can read about support for rem unit in caniuse documentation.

How does the use of rem unit improve responsive design?

rem unit is extremely beneficial for responsive design. Since rem is relative to the root element, changing the root font-size allows you to adjust the size of all elements defined in rem. This can be done by using media queries, allowing to make different font sizes for different screen breakpoints.

What is the impact of using rem units on browser accessibility?

Using rem unit can significantly improve the accessibility of website. Some users may adjust their browser's default font size for better reading experience. Since rem unit is relative to this base font-size, it allows the layout and spacing of your website to adjust according to the user's preferences, which improves the UX.

Can I use rem unit in combination with another unit?

Yes, rem unit can be used in combination with another CSS unit. It can provide more flexibility in your design. For example, you may use px for border-width (which don't need to scale with text size) and rem unit for padding (which does).

How do I set the base font size for rem unit?

The base font-size for rem unit is set on the root element (html) by using font-size style property. For example, if you want the base font-size to be 20px, you might use: html { font-size: 20px; }. All rem units will be using that size.

What are the best practices for using rem unit?

Some best practices for using rem unit in responsive web design include setting a sensible base font size on the root element, using rem unit for properties that need to scale with text size, and testing the design at different base font sizes to ensure it remains accessible.