A brief analysis of the differences between px, rem, em, vh, and vw in CSS

A brief analysis of the differences between px, rem, em, vh, and vw in CSS

Absolute length

px

px is the pixel value, which is a fixed length, like our meters and centimeters.

Relative length

Why do we need relative lengths like rem em etc?

Fixed length can no longer meet our current needs.

For example: When we reduce the size of our screen, we not only need to reduce the width and height of our box, but we also want to reduce the font size as well, so that the user experience will be better.

rem

The calculation relationship between rem and px

The value of rem is a multiple of px

By default, font-size = 16px, so 1rem = 16px

How to modify the relative calculation relationship between rem and px

We can and can only modify font-size: 32px in the html tag (because the html node is the root node, which is the r:root in rem), so 1rem = 32px

Code

<div class="div-rem">rem</div>
/* Usage of rem */
html{
    font-size:16px; // 1rem = 16px
}
.div-rem{
    width: 10rem; // 10rem = 10 x 16 = 160px
    height: 10rem; // 10rem = 10 x 16 = 160px
    font-size: 1rem; // 1rem = 16px
    background-color: #a58778;
}

em

The calculation relationship between em and px

The value of em is a multiple of px

By default, font-size = 16px, so 1em = 16px

How to modify the relative calculation relationship between em and px

We can modify the font-size on our element: 32px, so 1em = 32px

If the font-size is not set on our element, we can also set the font-size on the parent element to affect the em value used by our element (child element).

The difference between rem and em

Summarize

This is the end of this article about the differences between the units px, rem, em, vh, vw in CSS. For more information about the differences between px, rem, em, vh, vw, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Why MySQL chooses Repeatable Read as the default isolation level

>>:  A very detailed explanation of Linux C++ multi-thread synchronization

Recommend

The homepage design best reflects the level of the web designer

In the many projects I have worked on, there is b...

CSS3 sample code to achieve element arc motion

How to use CSS to control the arc movement of ele...

How to view available network interfaces in Linux

Preface The most common task after we install a L...

MySQL Innodb key features insert buffer

Table of contents What is insert buffer? What are...

In-depth explanation of iterators in ECMAScript

Table of contents Preface Earlier iterations Iter...

Sample code for installing ElasticSearch and Kibana under Docker

1. Introduction Elasticsearch is very popular now...

Solution for VMware Workstation Pro not running on Windows

After the National Day holiday, did any of you fi...

How to use Javascript to generate smooth curves

Table of contents Preface Introduction to Bezier ...

Detailed explanation of how to use CMD command to operate MySql database

First: Start and stop the mysql service net stop ...

URL representation in HTML web pages

In HTML, common URLs are represented in a variety ...

About the problem of dynamic splicing src image address of img in Vue

Let's take a look at the dynamic splicing of ...

Advanced techniques for using CSS (used in actual combat)

1. The ul tag has a padding value by default in Mo...

How to run JavaScript in Jupyter Notebook

Later, I also added how to use Jupyter Notebook i...