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

Storage engine and log description based on MySQL (comprehensive explanation)

1.1 Introduction to storage engines 1.1.1 File sy...

Docker builds CMS on-demand system with player function

Table of contents text 1. Prepare the machine 2. ...

Detailed explanation of Mysql self-join query example

This article describes the Mysql self-join query....

Analysis of Apache's common virtual host configuration methods

1. Apache server installation and configuration y...

An Incomplete Guide to JavaScript Toolchain

Table of contents Overview Static type checking C...

How to install docker under centos and remotely publish docker in springboot

Table of contents 1. Installation of JDK1.8 under...

Detailed explanation of the flexible use of CSS grid system in projects

Preface CSS grids are usually bundled in various ...

HTML special character conversion table

character Decimal Character Number Entity Name --...

Example code for Html layered box-shadow effect

First, let’s take a look at the picture: Today we...

How to handle forgotten passwords in Windows Server 2008 R2

What to do if you forget Windows Server 2008R2 So...

Win2008 Server Security Check Steps Guide (Daily Maintenance Instructions)

The document has been written for a while, but I ...

Detailed usage of kubernetes object Volume

Overview Volume is the abstraction and virtualiza...

JavaScript implements the generation of 4-digit random verification code

This article example shares the specific code for...

Writing daily automatic backup of MySQL database using mysqldump in Centos7

1. Requirements: Database backup is particularly ...