Summary of differences between px, em, and rem in css, pxrem

Source: Internet
Author: User

Summary of differences between px, em, and rem in css, pxrem

Preface

Both em and rem are flexible and scalable units, which are converted from a browser to a pixel value, depending on the font size in the design. If the value is 1em or 1rem, it can be converted from 16px to 160px or any other value by the browser. If the browser uses 1px, 1px is always displayed as full 1px.

Similarities between em and rem

The use of em and rem units makes our design more flexible, allowing us to control the overall enlargement and reduction of elements, rather than the fixed size.

Em and rem differences

The difference is who the browser converts to px values.

How is the rem unit converted to a pixel value?

When rem units are used, their conversion to pixel size depends on the font size of the page root element, that is, the font size of the html element. Multiply the font size of the root element by your rem value.

For example, the font size of the root element is 16 PX and 10 REM is equivalent to 160 PX, that is, 10x16 =.

How to convert em units to pixel values

When the em unit is used, the pixel value is the em value multiplied by the font size of the element in the em unit. For example, if a div has a 18 PX font size, 10em is equivalent to 180px, that is, 10 × 18 = 180.

Important:

There is a common misunderstanding that the em unit is the font size relative to the parent element. In fact, according to the W3 standard, they are the font size relative to the elements using the em unit. The font size of the parent element can affect the em value, but this happens purely because of inheritance. Let's see why and how it works.

What you need to know:

The root html element inherits the font size set in the browser, unless the fixed value is explicitly set to overwrite. Therefore, although the html element's font size is directly determined by the rem value, the font size may first come from the browser settings. Therefore, the font size setting of the browser affects each rem unit and each value inherited from the em unit.

Summary and rem difference em

All of the above are as follows:

Why use rem units:

Rem units provide the greatest power not only by providing consistent dimensions rather than inheritance. Instead, it gives us a way to get user preferences to influence the size of each element on the website that uses rem, instead of using a fixed px Unit.
To this end, the main purpose of using the rem unit should be to ensure that our layout can be adjusted to a proper size regardless of how users set their own browsers.

Why use em unit

The em unit is determined by the font-size value rather than the font size of the html element.

To this end, the main purpose of the em unit should be to allow scalability within a specific design element range.

For example, you may use the em value to set the padding, margin, and line-height values of the navigation menu items. Menus with 0.9rem font size

In this way, if you change the font size of the menu, the spacing around the menu items will be scaled proportionally in the remaining space.

Summary

  • The rem and em units are pixel values calculated by the browser based on the font size in your design.
  • The em unit is based on the font size of their elements.
  • The rem unit is based on the font size of the html element.
  • The em unit may be affected by the font size of any inherited parent element.
  • The rem unit can inherit the font size from the browser font settings.
  • The em unit should be used based on the font size of the component rather than the font size of the root element.
  • Use rem when you do not need to use em units, and you need to set Scaling Based on the browser font size.
  • Use rem units unless you are sure you need em units, including the font size.
  • Rem units used in media queries
  • Do not use em or rem-to change to % in Multi-column layout.
  • Do not use em or rem. Scaling will inevitably lead to breaking the layout element.

The above is all about the differences between px, em, and rem in css. I hope the content in this article will help you in your study or work, if you have any questions, you can leave a message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.