I believe every front-end engineer has this experience. Most domestic designers like to use px, while most foreign websites like to use em and rem. So what is the difference between the three, and what are the advantages and disadvantages of each? ? In the following article, the editor will give you a detailed introduction to the differences between px, em and rem in css. Friends in need can refer to it. Let's take a look together.
Preface
em and rem are flexible and scalable units, converted to pixel values by the browser, depending on the font size in the design, if using the value 1em Or 1rem, which can be converted by the browser from 16px to 160px or any other value. Browsers use 1px , so 1px is always displayed as exactly 1px.
The similarities between em and rem
Using em and rem units can make our design more flexible and control the overall enlargement and reduction of elements instead of fixed sizes
The difference between em and rem
The difference is who the browser converts into px value
How the rem unit is converted into pixel value
When using rem units, their conversion to pixel size depends on the font size of the page root element, that is, the font size of the html element. The root element font size is multiplied by your rem value.
For example, the font size of the root element is 16px, 10rem will be equivalent to 160px, that is, 10 x 16 = 160.
How em units are converted to pixel values
When using em units, the pixel value will be the em value multiplied by the font size of the element using em units. For example, if a p has a font size of 18px, 10em will be equivalent to 180px, which is 10 × 18 = 180.
Key understanding:
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 relative to the font size of the element using em units. The font size of the parent element can affect em values, but this happens purely because inherits. Let's see why and how it works.
What you need to know:
The root html element will inherit the font size set in the browser, unless a fixed value is explicitly set to override it. So although the font size of the html element is directly determined by the rem value, the font size may first come from the browser settings. So the browser's font size setting can affect every value that uses rem units and every value inherited through em units.
Summary of the differences with rem
All the above boils down to the following:
rem units translated into pixel values are determined by the html element Determined by the font size. This font size is affected by the font size setting in the browser unless a specific unit is explicitly overridden.
em units are converted to pixel values, depending on the font size they are using. This font size is governed by the font size inherited from the parent element, unless explicitly overridden with a specific unit.
Why use rem units:
Rem units provide the greatest power not just because they provide consistent size rather than inheritance. Instead, it gives us a way to obtain user preferences and influence the size of elements using rem everywhere on the website, instead of using fixed px units.
For this reason, the main purpose of using rem units should be to ensure that our layouts resize to the appropriate size no matter how users set up their browsers.
Why use em units
The em unit depends on a 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 extensibility to remain within the scope of a specific design element.
For example, you may use em values to set the padding, margin, line-height and other values of Navigationmenu items. Menu with 0.9rem font size
This way, if you change the menu's font size the spacing around the menu items will scale proportionally in the remaining space.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How does CSS handle the browser’s default style
CSS implements visible progress bar with arrow process
The above is the detailed content of Detailed explanation of the use of px, em and rem in css. For more information, please follow other related articles on the PHP Chinese website!

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools
