em is a relative length unit in CSS, relative to the font size of the text in the current object; if the current font size of the inline text has not been manually set, it is relative to the browser's default font size. It can be used to set width, height, line-height, margin, border and other styles.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
There are many units in CSS, the commonly used px is the absolute unit, and em is the relative unit. Under the premise of responsiveness and mobile terminals, using em can more conveniently and quickly adjust a series of attributes such as font size, width, margin, and border of web documents and HTML elements at once. It can be said that in some aspects, using em as a Units are more flexible than px.
em It is a relative unit in CSS, and its unit length is determined based on the vertical length of the element's text. It can be used to set width, height, line-height, margin, padding, border and other styles.
1em=元素中文本的1个垂直高度
According to the above rules: if the size of the text in the element is 16px, then 1em=16px; if the size of the text in the element is 20px, then 1em=20px...
The text in the browser generally defaults to 16px, that is, by default:
1em=16px
How to change this What about settings? Just explicitly set the font-size of the body element. eg:
body { font-size: 24px; width: 10em; /* 10em = 24px * 10 = 240px */ }
In CSS, if an element does not set font-size, then its font-size value is its parent element The font-size value is easy to understand, it is just a simple inheritance. eg:
<style> body { font-size: 12px; } div { /* 该元素没有设置font-size,因此继承了父元素的font-size: 12px; */ width: 10em; /* 10em = 12px * 10 = 120px */ } </style> <body> <div></div> </body>
It should be noted that the child element p inherits the font-size of the parent element body, so in fact, the sentence "font-size: 12px;" is implicit in p's style sheet. Now if you explicitly set font-size for a child element, the child element will calculate the absolute length of em according to its own font-size. eg:
<style> body { font-size: 12px; } div { font-size: 20px; width: 10em; /* 10em = 20px * 10 = 200px */ } </style> <body> <div></div> </body>
Note that in the above example, the font-size of p uses px as the unit, so what if you want to use em? It should be noted that if em is used as the unit when setting font-size, then em will be the relative value of the font-size of its parent element. eg:
<style> body { font-size: 12px; } div { font-size: 2em; /* 2em = 12px * 2 = 24px */ width: 10em; /* 10em = 24px * 10 = 240px */ } </style> <body> <div></div> </body>
The font-size of the child element p is determined based on the font-size of its parent element body, so 2em = 12px * 2, = 24px; and the width of p is relative to its own font- The size is determined, so 10em = (12px * 2) * 10 = 240px. So it’s not surprising that 2em=24px and 10em=240px in p.
In fact, not only the width, but also the font-size em in the child elements are determined based on the font-size of the parent element, and all other ems are determined based on their own font-size.
<style> body { font-size: 16px; } div { font-size: 1.25em; /* 1.25em = 16px * 1.25 = 20px */ width: 10em; /* 10em = 20px * 10 = 200px */ height: 5em; /* 5em = 20px * 5 = 100px */ border: 0.05em solid #000; /* 0.05em = 20px * 0.05 = 1px */ margin: 0.25em; /* 0.25em = 20px * 0.25 = 5px */ padding: 0.75em; /* 0.75em = 20px * 0.75 = 15px */ line-height: 1.5em; /* 1.5em = 20px * 1.5 = 30px */ } </style> <body> <div></div> </body>
(Learning video sharing: css video tutorial)
<style> div { font-size: 16px; width: 2em; /* 2em = 16px * 2 = 32px */ } </style>
px = Reference text size* em => em = px / Reference text size
Programming Video! !
The above is the detailed content of What does em mean in css. For more information, please follow other related articles on the PHP Chinese website!