search
HomeWeb Front-endCSS TutorialA brief discussion on css length units: em, px, rem

The content of this article is a brief discussion of CSS length units: en, px, rem. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Use em or px to set the font size

CSS inherits the units pt (point) and pc (pica) from typesetting. Used on traditional printers and takes precedence over similar units cm or in. There are reasons in CSS not to use the unit pt, you can use any unit you like. We have a good reason to use neither pt nor any other absolute unit (), and that is to use em and px.

There are several rows of different thicknesses here. Some or all of them may look sharp, but at least the 1px and 2px lines should be clearly visible:

A brief discussion on css length units: em, px, rem

if the first four lines all look the same (or if 0.5 is missing pt line), you may be looking at a computer monitor that cannot display points smaller than 1px. If the thickness of the lines increases, you may be viewing this page on a high-quality computer screen or paper. If 1pt looks thicker than 1.5px, you probably have a handheld screen.

CSS’s magic unit: px, is often a good unit to use, especially if the style requires text to align with an image, or simply because 1px wide or a multiple of 1px guarantees a sharp look.

But for font size it's even better to use em. The idea is to (1) not set the font size of the BODY element (in HTML), but use the device's default size, since that's what readers can comfortably read; (2) represent the font size of other elements em: H1 { font-size: 2.5em} makes H1 2½ times the normal body font.

The only place you can use pt (or cm or in) for setting the font size here is in the stylesheet for printing, if you need to make sure the printed font is exactly a certain size. But even using the default font size is usually better.

Because, the px unit prevents you from knowing the resolution of the device. Whether the output is 96dpi, 100dpi, 220dpi or 1800dpi, it's expressed in integer px lengths and will always look good and be very similar on all devices. But what if you want to know the resolution of your device, for example, to know if it's safe to use 0.5px lines? The answer is to check the resolution via media queries. Explaining media queries is beyond the scope of this article, but here is a small example:

div.mybox {
   border:2px solid;
}
@media(min-resolution:2dppx){
  / *每px有2个或更多点的介质* /
  div.mybox {
     border:1.5px solid;
  }
}

2. New units for css: rem

To make it easier For writing style rules that rely only on the default font size, CSS has a new unit since 2013: rem. rem (for "root em") is the font size of the document's root element. The difference from em elements is that rem is unchanged throughout the document. For example, to give the p and h1 elements the same left margin, compare the pre-2013 stylesheet:

p {margin-left:1em}
h1 {font-size:3em; margin-left:0.333em}

New version:

p {margin-left:1rem}
h1 {font-size:3em; margin-left:1rem}

css There are some other new units that can be specified relative to The size of the reader window, this is vw and vh. vw is 1/100 of the window width and vh is 1/100 of the window height. There is also vmin, which represents the smallest of vw and vh. vmax represents the largest of vw and vh.

Because they are so new, there is no way they can be used anywhere yet. However, as of early 2015, some browsers support them.

Summary: The above is the em, px, rem and other length units in CSS introduced in this article. I hope it will be helpful to everyone's learning.

The above is the detailed content of A brief discussion on css length units: em, px, rem. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
REM(全称REMME)是什么币?REM(全称REMME)是什么币?Feb 21, 2024 pm 05:00 PM

REMME是什么币?REMME是一个基于区块链技术的加密货币,致力于提供高度安全且去中心化的网络安全和身份验证解决方案。该项目旨在利用分布式加密技术来增强和简化用户身份验证流程,从而提升安全性和效率。REMME的创新之处在于其借助区块链的不可篡改性和透明性,为用户提供了更可靠的身份验证方式。通过将身份验证信息存储在区块链上,REMME消除了中心化身份验证系统的单点故障,并降低了数据被盗或篡改的风险。这种基于区块链的身份验证方法不仅更安全可靠,而且还能够为用户REMME的背景在当前数字化时代,网络

CSS布局单位的演变与应用:从像素到根据根元素字体大小的相对单位CSS布局单位的演变与应用:从像素到根据根元素字体大小的相对单位Jan 05, 2024 pm 05:41 PM

从px到rem:CSS布局单位的演变与应用引言:在前端开发中,我们经常需要用到CSS来实现页面布局。在过去的几年间,CSS布局单位也经历了演变和发展。最开始我们使用的是像素(px)作为单位来设置元素的大小和位置。然而,随着响应式设计的兴起和移动设备的普及,像素单位逐渐暴露出一些问题。为了解决这些问题,新的单位rem应运而生,并逐渐被广泛应用于CSS布局中。一

CSS 单位属性优化技巧:em,rem,px 和 vw/vhCSS 单位属性优化技巧:em,rem,px 和 vw/vhOct 20, 2023 pm 12:54 PM

CSS单位属性优化技巧:em,rem,px和vw/vh引言:在网页设计和开发中,CSS单位属性起着非常重要的作用。正确选择和使用合适的单位属性可以使得页面在不同的设备和屏幕尺寸下展示得更加美观和一致。本文将介绍一些常用的CSS单位属性,并提供具体的代码示例来帮助读者更好地掌握这些优化技巧。em单位:em单位是相对于父元素的字体大小来计算的。例如

css中的em是什么单位css中的em是什么单位Dec 07, 2023 pm 02:56 PM

CSS中的em是一个相对长度单位,它是相对于元素的字体大小来计算的,1em等于当前元素的字体大小,如果应用在字体大小上,1em将等于父元素的字体大小。

html5中px和em的区别是什么html5中px和em的区别是什么Aug 19, 2022 pm 05:36 PM

区别:1、单位长度不同,px是数字化图像长度单位,em是字符宽度的倍数;2、相对对象不同,px是相对于显示器屏幕分辨率而言的,em是相对于当前对象内文本的字体尺寸。3、px的值是固定的,指定是多少就是多少,计算比较容易;em的值不是固定的,并且em会继承父级元素的字体大小。

CSS 单位属性指南:em,rem,px 和 vw/vhCSS 单位属性指南:em,rem,px 和 vw/vhOct 25, 2023 am 10:37 AM

CSS单位属性指南:em,rem,px和vw/vh在编写CSS样式时,选择合适的单位属性是非常重要的。本文将介绍几种常用的单位属性:em,rem,px和vw/vh,并提供具体的代码示例。emem(字体尺寸单位)是相对于父元素字体尺寸的单位。如果父元素的字体尺寸为16px,1em就等于16px。当em用于其他属性(如宽度、高度等)时,也是相对于父元素

实现响应式布局的自适应效果,应该使用哪些单位?实现响应式布局的自适应效果,应该使用哪些单位?Jan 27, 2024 am 09:47 AM

在响应式布局中,使用何种单位来实现自适应的效果?随着移动设备的普及和各种尺寸的屏幕出现,响应式布局成为了现代网页设计与开发中一个重要的概念。通过响应式布局,可以使网页在不同设备上实现自适应的效果,提升用户体验。而在实现响应式布局的过程中,选择合适的单位来进行布局是非常重要的。本文将介绍一些常用的单位,并讨论它们在不同场景下的适用性。首先,我们来讨论一下最常见

Vue移动端解决1px像素问题的方法Vue移动端解决1px像素问题的方法Jun 30, 2023 pm 06:21 PM

Vue开发中如何解决移动端1px像素问题随着移动互联网的快速发展,移动端应用的需求日益增加。然而,移动设备屏幕的尺寸和像素密度的多样性给开发者带来了一定的挑战。其中一个常见的问题是移动端1px像素问题。本文将介绍如何在Vue开发中解决移动端1px像素问题。问题的根源移动端1px像素问题的根源在于移动设备的物理像素和设备独立像素的不匹配。设备独立像素(CSS像

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft