What is em? Introduction and conversion method of em and px

What is em? Introduction and conversion method of em and px
What is em?

em refers to the font height, and the default font height of any browser is 16px. So all unadjusted browsers will conform to: 1em=16px. Then 12px=0.75em, 10px=0.625em. To simplify font-size conversion, you need to declare Font-size=62.5% in the body selector in CSS. This will change the em value to 16px*62.5%=10px, so 12px=1.2em, 10px=1em. In other words, you only need to divide your original px value by 10 and then use em as the unit.

em has the following characteristics:

1. The value of em is not fixed;
2. em will inherit the font size of the parent element.

Rewrite steps:

1. Declare Font-size=62.5% in the body selector;
2. Divide your original px value by 10, and then use em as the unit;
It’s simple, right? If the problem can be solved with just the above two steps, no one would use px. After the above two steps, you will find that the fonts on your website are larger than you imagined. Because the value of em is not fixed and will inherit the size of the parent element, you may set the font size in the content div to 1.2em, which is 12px. Then you set the font size of the selector p to 1.2em, but if p is a child of content, the font size of p is not 12px, but 1.2em = 1.2 * 12px = 14.4px. This is because the font size of content is set to 1.2em. This em value inherits the size of its parent element body, which is 16px * 62.5% * 1.2=12px. As p is its child, em inherits the font height of content, which is 12px. So 1.2em of p is no longer 12px, but 14.4px.
3. Recalculate the em values ​​of the fonts that are enlarged. Avoid repeated declarations of font sizes, that is, avoid the phenomenon of 1.2 * 1.2 = 1.44 mentioned above. For example, if you declare the font size in #content as 1.2em, then when declaring the font size of p, it can only be 1em, not 1.2em, because this em is not that em. It inherits the font height of #content and becomes 1em=12px.

<<:  Creating private members in JavaScript

>>:  Detailed explanation of the installation, configuration, startup and shutdown methods of the Mysql server

Recommend

JavaScript to implement checkbox selection or cancellation

This article shares the specific code of JavaScri...

JS implements a simple counter

Use HTML CSS and JavaScript to implement a simple...

Detailed explanation of HTML programming tags and document structure

The purpose of using HTML to mark up content is t...

MySQL 5.7.27 installation and configuration method graphic tutorial

The installation tutorial of MySQL 5.7.27 is reco...

Several ways to encrypt and decrypt MySQL (summary)

Table of contents Written in front Two-way encryp...

mysql error number 1129 solution

SQLyog connects to mysql error number 1129: mysql...

Solution to the same IP after cloning Ubuntu 18 virtual machine

Preface I recently used a virtual machine to inst...

Docker installation and deployment example on Linux

After reading the following article, you can depl...

Modify file permissions (ownership) under Linux

Linux and Unix are multi-user operating systems, ...

Use JS to zoom in and out when you put the mouse on the image

Use JS to zoom in and out when the mouse is on th...

A friendly alternative to find in Linux (fd command)

The fd command provides a simple and straightforw...

mysql8.0.11 winx64 installation and configuration tutorial

The installation tutorial of mysql 8.0.11 winx64 ...

A Preliminary Study on JSBridge in Javascript

Table of contents The origin of JSBridge The bidi...

Detailed tutorial on installing Prometheus with Docker

Table of contents 1. Install Node Exporter 2. Ins...