A Complete Guide of responsive web design using CSS rem and em Units
Tags: css, webdesign, responsive, cssunits
Published: 2021-05-18 08:15:57

<p>Nowadays, responsive design is an important part of any website. It helps adjust your site on any screen. Modern CSS makes it easy for designers to build a responsive website with the help of <b>`media` rules.</b> <p>

<p>This article will learn together on how to build a responsive website with two CSS measuring units with complete explanations and examples.</p>

<p>Both <b>em</b> and <b>rem</b> are flexible, scalable units that are translated by the browser into pixel values, depending on the font size settings in your design.</p>

<p>As I mentioned above <b>rem & em</b> units are translated into pixels. Pixel is the main unit and fixed unit to measure each screen size.</p>

<h4>How REM Unit Works?</h4>
<p>REM units are based on the root element. When using rem units, the pixel size they translate to depends on the font size of the root element of the page, <b>i.e</b>, the HTML element. That root font size is multiplied by whatever number you’re using with your rem unit.<p>
<br>
<p><b>Note:</b> 1rem = 16px</p>
<br>
<p>In modern browsers the root font-size value is 16px, hence, 1rem = 16px.</p>

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fuqtluqgroen92noxaiu.png" alt="rem-output"> 
<h4>Where to use REM Unit?</h4>
<p>REM Unit provides flexibility in our design. REM Unit 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.</p>

<p>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.</p>

<p>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.</p>

<h4>How EM Unit Works?</h4>
<p>
EM Unit is based on the font size of the same element where it is defined, if the same element doesn’t have font-size defined, they will inherit their parent, or from another ancestor element, possibly going all the way back to the root of the document. When using em units, the pixel value is multiplied by the font size of the given element.
</p>

![CSS EM UNIT OUTPUT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nefcpra8a3qkm1g3x83j.png)

<br>

![CSS EM UNIT OUTPUT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5m9tkzbilif8641vf7p.png)


<h4>Where to use EM Unit?</h4>
<p>The key-value em units offer is they allow sizing values to be determined by a font size other than that of the HTML element. The primary purpose of em units should be to allow for scalability within the context of a specific design element.</p>

<p><b>e.g</b>, you might set the padding, margin and line-height around navigation, menu item to use em values. This way if you change the menu’s font size the spacing around the menu items will scale proportionately, independently of the rest of the layout.</p>

<p>As per our example above, design components like menu items, buttons, and headings may have their own explicitly stated font sizes. If you change these font sizes, you want the entire component to scale proportionately.</p>

<p>Common properties this guideline will apply to are margin, padding, width, height, and line-height settings when used on elements with non-default font sizing.</p>

<h4>Responsive web design using REM & EM Units</h4>
Now that we have understood everything about REM & EM Units and how they work. It’s time to see how we use this flexibility to build a responsive website.<p>

<p>Most of the spacing and sizing are adjusted by the REM unit by defining the font in the root element.<p>

<p>An important thing to keep in mind is that always use percentage values when defining the font-size value in the root element, e.g, HTML. The benefit of using percentage over pixels is that our root element is no longer has a fixed hard-coded value.</p>

```
html{
   font-size: 62.5%; // 10px 
}
```

Always use the em unit while working with media queries because it works well in all browsers and the rem unit somehow failed in some browsers. Also, an em-based approach to media queries allows for a more proportional measurement and layout that adjust based on font size.

```
@media(max-width: 68.75em){ ... }  // 1100px
```

I found a very good resource by webdesign.tutsplus.com’s article which mentioned some good key points which are very useful:

* rem and em units are computed into pixel values by the browser, based on font sizes in your design.
em units are based on the font size of the element they’re used on.
* rem units are based on the font size of the HTML element.
em units can be influenced by font size inheritance from any parent element
* rem units can be influenced by font size inheritance from browser font settings.
* Use em units for sizing that should scale depending on the font size of an element other than the root.
* Use rem units for sizing that doesn’t need em units, and that should scale depending on browser font size settings.
Use rem units unless you’re sure you need em units, including font sizes.
* Use rem units on media queries
* Don’t use em or rem in multi-column layout widths – use % instead.
* Don’t use em or rem if scaling would unavoidably cause a layout element to break.

After reading the whole article with small examples of code, this is will be cleared that how flexible em & rem units are in CSS. and how we build a better responsive using these two units in our design.