Home Web Front-end CSS Tutorial The difference between px, em and rem

The difference between px, em and rem

Dec 12, 2018 am 10:52 AM
em px rem

They are all used to set the size of the font and the width and height of the box, but px will not change due to changes in browser size, while em and rem will change due to changes in browser size.

In the process of writing code, we often use size units when defining the size of fonts or the width and height of elements in CSS. Today we will introduce in detail the common units in CSS. The name of the size unit and its usage have certain reference value. I hope it will be helpful to everyone.

【Recommended course: CSS Tutorial

The difference between px, em and rem

##px

px is the abbreviation of pixel, which means pixel. It is used when specifying the font size and the width and height of the element. Pixels are relative to the monitor screen resolution

Example: Set the width to 200px and the height to 200px for a div element

div{
width:200px;
height:200px;
border: 1px solid #ccc;
text-align: center;
line-height: 200px;
font-size: 16px;

		}
Copy after login

The effect is as follows:

The difference between px, em and rem

em

em is a relative length unit, which is relative to the font size of the text within the current object. If we have not set the font size of the current text, then em will be relative to the default font size of the browser

The default font size in the browser is 16px, in other words 1em=16px, generally when we write Em units are often used in adaptive layout. Simplify the code by setting the font-size value in the body selector in CSS so that all ems on the page are relative to the body value.

Example: Set the width to 100px and the height to 100px for the div element by changing the size unit to em

1em=16px, so 100px=6.25em

<style>
div{
width:6.25em;
height:6.25em;
border: 0.0625em solid #ccc;
text-align: center;
line-height: 6.25em;
}
</style>
Copy after login

Effect Picture:

The difference between px, em and rem

We can also set a value directly to the body so that its value is directly relative to the body, and then divide the original px value by 10. The value of em

body{
font-size: 62.5%
	}
div{
width:10em;
height:10em;
border:0.1em solid #ccc;
}
</style>
Copy after login

Rendering:

The difference between px, em and rem

It can be seen from the above figure that the value of em is not fixed and is relative to his parent Level element size

#rem:

rem is a new relative unit in CSS3. The difference between it and em is that when using rem to set the font size for an element, Still a relative size, but relative only to the HTML root element. Its use is very simple. You can change its value by changing the size of the root element.

Example: Set the width to 100px and the height to 100px for the div element by changing the size unit to rem

body{
		font-size:10px;
	}
		div{
			width:15rem;
			height:15rem;
			border:0.01rem solid pink;
			text-align: center;
			line-height: 15rem;
			font-size: 2rem;
		}
Copy after login

Rendering:

The difference between px, em and rem

Summary: The above is the entire content of this article, I hope it will be helpful to everyone.


The above is the detailed content of The difference between px, em and rem. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is REM (full name REMME)? What is REM (full name REMME)? Feb 21, 2024 pm 05:00 PM

What coin is REMME? REMME is a cryptocurrency based on blockchain technology dedicated to providing highly secure and decentralized network security and identity verification solutions. The project aims to use distributed encryption technology to enhance and simplify the user authentication process, thereby improving security and efficiency. The innovation of REMME is that it uses the immutability and transparency of the blockchain to provide users with a more reliable identity verification method. By storing authentication information on the blockchain, REMME eliminates the single point of failure of centralized authentication systems and reduces the risk of data theft or tampering. This blockchain-based authentication method is not only more secure and reliable, but also provides users with the background of REMME. In the current digital era, the network

The evolution and application of CSS layout units: from pixels to relative units based on the font size of the root element The evolution and application of CSS layout units: from pixels to relative units based on the font size of the root element Jan 05, 2024 pm 05:41 PM

From px to rem: The evolution and application of CSS layout units Introduction: In front-end development, we often need to use CSS to implement page layout. Over the past few years, CSS layout units have evolved and developed. Initially we used pixels (px) as the unit to set the size and position of elements. However, with the rise of responsive design and the popularity of mobile devices, pixel units have gradually exposed some problems. In order to solve these problems, the new unit rem came into being and was gradually widely used in CSS layout. one

CSS unit property optimization tips: em, rem, px and vw/vh CSS unit property optimization tips: em, rem, px and vw/vh Oct 20, 2023 pm 12:54 PM

CSS unit attribute optimization tips: em, rem, px and vw/vh Introduction: In web design and development, CSS unit attributes play a very important role. The correct selection and use of appropriate unit attributes can make the page display more beautiful and consistent on different devices and screen sizes. This article will introduce some commonly used CSS unit properties and provide specific code examples to help readers better master these optimization techniques. em unit: em unit is calculated relative to the font size of the parent element. For example

What is the unit of em in css? What is the unit of em in css? Dec 07, 2023 pm 02:56 PM

Em in CSS is a relative length unit, which is calculated relative to the font size of the element. 1em is equal to the font size of the current element. If applied to the font size, 1em will be equal to the font size of the parent element.

What is the difference between px and em in html5 What is the difference between px and em in html5 Aug 19, 2022 pm 05:36 PM

Differences: 1. The unit length is different, px is the unit of digital image length, and em is a multiple of the character width; 2. The relative objects are different, px is relative to the monitor screen resolution, and em is relative to the font of the text in the current object. size. 3. The value of px is fixed, it is whatever you specify, and the calculation is easier; the value of em is not fixed, and em will inherit the font size of the parent element.

Which units should be used to achieve the adaptive effect of responsive layout? Which units should be used to achieve the adaptive effect of responsive layout? Jan 27, 2024 am 09:47 AM

In responsive layout, what kind of units are used to achieve adaptive effect? With the popularity of mobile devices and the emergence of screens of various sizes, responsive layout has become an important concept in modern web design and development. Through responsive layout, web pages can achieve adaptive effects on different devices and improve user experience. In the process of implementing responsive layout, it is very important to choose appropriate units for layout. This article will introduce some commonly used units and discuss their applicability in different scenarios. First, let’s discuss the most common

Guide to CSS unit properties: em, rem, px and vw/vh Guide to CSS unit properties: em, rem, px and vw/vh Oct 25, 2023 am 10:37 AM

Guide to CSS unit properties: em, rem, px and vw/vh When writing CSS styles, it is very important to choose the appropriate unit properties. This article will introduce several commonly used unit attributes: em, rem, px and vw/vh, and provide specific code examples. emem (font size unit) is a unit relative to the font size of the parent element. If the font size of the parent element is 16px, 1em is equal to 16px. When em is used for other attributes (such as width, height, etc.), it is also relative to the parent element

How to solve the 1px pixel problem on Vue mobile terminal How to solve the 1px pixel problem on Vue mobile terminal Jun 30, 2023 pm 06:21 PM

How to solve the 1px pixel problem on the mobile side in Vue development. With the rapid development of the mobile Internet, the demand for mobile applications is increasing day by day. However, the diversity of mobile device screen sizes and pixel densities poses certain challenges for developers. One of the common problems is the 1px pixel problem on mobile. This article will introduce how to solve the 1px pixel problem on the mobile side in Vue development. The root of the problem The root of the 1px pixel problem on the mobile side lies in the mismatch between the physical pixels of the mobile device and the device-independent pixels. Device independent pixels (CSS like

See all articles