CSS Rem: Understanding and Using rem Units

Obtain a comprehensive understanding of rem unit in CSS. Learn how rem units work and how to effectively use them to create visually appealing scalable, consistent, and accessible designs.

css rem unit

What is Rem in CSS?

Rem stands for "root em", which is a CSS unit of measurement relative to the font-size of the root element (usually the HTML element). This means that the value of a rem unit is equal to the font size of the root element, which is usually 16px by default for most browsers. The use of rem units allows for scalability, consistency, and accessibility throughout your CSS designs.

Understanding How Rem Units in CSS

Rem units work by defining the value of every instance of its occurrence in a CSS document in relation to the font size of the root element. By default, the font size of the root HTML element is 16px in most browsers.

For example, if the root element has a font size of 16 pixels, then 1 rem unit would equal 16 pixels. So an element defined as having a font size of 2 rem, would have a font size of 32 pixels. This makes it easy to create scalable designs that adjust automatically to different devices and screen sizes.

Rem units can be used to specify the size of elements such as font size, padding, margins, and widths. For example, the following CSS code specifies that the font size of a particular element (in this case the p tag) should be 1.5 rems and it should have a bottom margin spacing of 2 rem:

CSS

p { 
    font-size: 1.5rem;
    margin-bottom: 2rem;
}
                                                                    

This code would result in a font size of 24 pixels (1.5 rems x 16 pixels) and 32 pixels for the bottom margin spacing for that element, based on the assumption that the root element's font size is 16 pixels. By using rem units, designers and developers can create flexible and scalable designs that automatically adjust to different devices and screen sizes, without having to manually adjust the size of elements for each device or screen size.

Using rem Units in CSS

Setting the Font-Size of the Root Element

The font-size of the root element is the most important consideration when using rem units in CSS. The root element, usually represented by the HTML element or :root CSS pseudo-class, determine the size of all rem units in the CSS document.

Here are two examples of how to set the font-size of the root element in a CSS document:

CSS

html {
    font-size: 16px;
}
                                                                    

or

CSS

:root {
    font-size: 16px; /* recommended */
}
                                                                    

In these examples, the font-size of the root element is set to 16 pixels. All other font-sizes specified in rem units in the CSS document will be relative to this value. It's important to note that the font-size of the root element is 16px by default in most browsers. Therefore, you do not have to set the font-size of the root element unless you want to use a different default value other than 16px.

Also keep in mind that if you later decide to change the font-size of the root element, the value of all rem units defined in that CSS file will also change accordingly.

It's important to note that rem units can be used to specify values of several CSS properties, such as padding, margin, width, etc. To convert pixels to rem, divide the pixel value by the font-size of the root element to get the equivalent rem value.

Example of Using Rem Units in CSS Code

Here is a code pen example we created showing how to properly use rem units in CSS code. In this example, we have changed the default root font-size to 32px which means that 1rem now equals 32px instead of 16px. Take a look at both the HTML and CSS code to get a good understanding of how rem units are used.

Advantages of Using Rem Units

Scalability

Rem units are relative to the root element of the document, meaning that the size of a rem unit is determined by the font size of the root element. This makes it easy to create scalable and flexible designs that automatically adjust to different devices and screen sizes.

Consistency

By using rem units, designers and developers can ensure that the size and spacing of elements on a web page are consistent, even as the design adjusts to different devices and screen sizes. This helps to create a cohesive and visually appealing design.

Accessibility

Rem units can be used to create accessible designs that are easily usable by people with disabilities. By specifying font sizes in rem units, designers and developers can ensure consistent scaling so that the text on a web page is easily readable, regardless of the user's device or screen size.

Ease of maintenance

By using rem units, designers and developers can create a modular system of measurement that is easy to maintain and update.

For example, if you need to change the size of text elements on a web page, you can simply change the font-size of the root element, and all other text elements that are specified in rem units will automatically scale accordingly. This eliminates the need to manually adjust the font-size of each text element, which can be time-consuming and error-prone.

Better organization

Rem units can help to organize and structure CSS code by allowing designers and developers to create a single, unified system of measurement that is applied consistently across the entire web page. This helps to keep the code organized, readable, and maintainable, even as the design evolves.

In addition, using rem units can also help to simplify the design process by allowing designers and developers to create reusable components that can be easily incorporated into multiple pages or designs. This can save time and effort, as well as improve the consistency and quality of the design.

Disadvantages and Limitations of Using Rem Units

One of the challenges of using rem units in CSS is compatibility issues with older browsers. Some older browsers may not fully support rem units, which can result in unexpected layout or rendering issues.

For example, Internet Explorer 8 and earlier versions do not support rem units, which means that designers and developers need to use a different unit of measurement, such as pixels, in order to ensure compatibility with these older browsers.

In addition, some older browsers may have limited support for the font-size of the root element, which can impact the ability to control the scaling of elements on the page using rem units.

To overcome these compatibility issues, designers and developers may need to use a combination of different units of measurement, as well as test their designs on a variety of browsers and devices.

REM vs EM vs PX

Differences Between Rem, Em, and Pixels CSS Units

In CSS, there are three units commonly used to specify the size of elements as well as their spacing: rem, em, and pixels. Below are explanations of the differences between each of these CSS units of measurement especially rem vs em as well as which one you should use.

Rem units are relative to the font-size of the root element of the document. This means that, when using rem units, the size of an element is determined by the value of the root element's font-size, rather than by the size of the font of the parent element.

Em units are relative to the font-size of the parent element. For example, if an element let’s say a <div> has a font-size of 20 pixels, then a child element within that <div>, for example, a <p> with a font-size of 1em is equivalent to 20px. This means that, when using em units, the size of an element is dependent on the size of the parent element's font.

Pixels are an absolute unit of measurement in CSS and are not relative to any other value. A pixel is a fixed unit of length and will always have the same size, regardless of the size of the root element or parent element.

In summary, the difference between rem, em, and pixels is that rem units are relative to the font-size of the root element, em units are relative to the font-size of the parent element, and pixels are absolute units of measurement.

Should You Use Rem, Em, or Pixels CSS Units?

Rem is considered the ideal CSS unit of measurement to use in most cases because it is relative to the root element and not to the parent element. This makes rem units less complex to use than em units as it doesn’t have a compounding effect due to being relative to the root element. In more complex designs, rem units are far more predictable, especially for font sizes. It is also scalable and allows for accessibility.

On the other hand, em is relative to the parent element, which makes it great for specific use cases such as if you want proportional padding or margin between multiple child element variations in relation to their parent element. However, overuse of em units can become complex due to its compounding effect of being relative to the parent element. For example, if you have multiple child elements with different em values, with further nested child elements, changing the font-size value of the parent element may produce unexpected behavior. Therefore, em should be used in specific use cases.

Pixels (px) are an absolute unit of measurement that should be used when an element needs a fixed size or spacing, which can make it difficult to create a responsive accessible design. When using pixels, the size of an element is fixed and cannot be easily adjusted for different screen sizes, making it an inflexible option for web design. Therefore, px should be used sparingly.

In general, it's recommended to use rem primarily, with em used in specific cases where its cascading properties are needed, and px used sparingly, if at all, for specific cases where precise control is required.

Final Thoughts on Rem Units in CSS

The use of rem units in CSS is a powerful tool that can greatly improve the design and accessibility of a web page. When used correctly, rem units allow for consistent scaling of elements, improved accessibility through scalable text, and easier maintenance.

However, designers and developers need to be aware of the challenges and limitations of using rem units, such as compatibility issues with older browsers, the need to override the default font-size, and the need for a good understanding of CSS and the cascade.

Despite these challenges, the benefits of using rem units far outweigh the limitations, and it is a highly recommended tool for anyone working with CSS. By taking the time to understand rem units and how they work, designers and developers can take their CSS skills to the next level and create web pages that are both visually appealing and accessible to all users.