当前位置:网站首页>px和em和rem的区别
px和em和rem的区别
2022-08-02 14:12:00 【叶子yes】
px
px是像素单位,是相对屏幕宽高度而言,一旦设定就无法随浏览器尺寸的改变而改变。
示例:设置padding为30px,宽高100px
<style> div{
padding: 30px; width: 100px; height: 100px; border: 1px solid black; } </style>
<body>
<div>我是一个div</div>
</body>

em
em是相对长度单位,相对当前元素的字体大小而言。
示例:我们给div设置padding为1em
<style> div{
padding: 1em; border: 1px solid black; } </style>
<body>
<div>我是一个div</div>
</body>

可以看出默认 1em = 16px (因为默认字体大小为16px)
我们设置font-size为28px,再来看一下:
div{
font-size: 28px;
padding: 1em;
border: 1px solid black;
}

可以看出padding变成了28px,所以em设置的值不是固定的,会随当前元素的字体大小的变化而变化。
rem
rem也是一个相对长度单位,相对的是html元素的字体大小而言的。
示例:给div设置padding为1rem
<style> div{
padding: 1rem; border: 1px solid black; } </style>

这里1rem也是默认16px。
接下来,我们设置html的字体大小为20px:
html{
font-size: 20px;
}
div{
padding: 1rem;
border: 1px solid black;
}

可以看出padding变成了20px
我们可以再给div标签的文本设置文字大小为28px:
html{
font-size: 20px;
}
div{
padding: 1rem;
border: 1px solid black;
font-size: 28px;
}

可以看出,padding仍然是20px,所以rem相对的是html元素的字体大小而言的。
边栏推荐
- KiCad Common Shortcuts
- 基于最小二乘法的线性回归分析方程中系数的估计
- MATLAB绘图函数fplot详解
- Test case exercises
- 开心一下,9/28名场面合集
- General code for pytorch model to libtorch and onnx format
- Detailed introduction to drawing complex surfaces using the plot_surface command
- 极简式 Unity 获取 bilibili 直播弹幕、SC、上舰、礼物等 插件
- LeetCode 2344. 使数组可以被整除的最少删除次数 最大公约数
- Problems related to prime numbers - small notes
猜你喜欢

【离散化+前缀和】Acwing802. 区间和

Unity-PlayMaker

第二十六章:二维数组

7.Redis

Lightweight AlphaPose

Based on the matrix calculation in the linear regression equation of the coefficient estimates

Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment

Codeforces Round #605 (Div. 3)

Open the door to electricity "Circuit" (3): Talk about different resistance and conductance

UnityAPI-Ray-Physics
随机推荐
动态规划理论篇
第三十一章:二叉树的概念与性质
C语言函数参数传递模式入门详解
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
STM32LL library use - SPI communication
Installation and configuration of Spark and related ecological components - quick recall
Codeforces Round #624 (Div. 3)
Network Security Packet Capture
第三十三章:图的基本概念与性质
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
7. Redis
unity 和C# 一些官方优化资料
GMP scheduling model of golang
Mysql的锁
关于混淆的问题
shader 和 ray marching
MATLAB绘制平面填充图入门详解
Yolov5 official code reading - prior to transmission
unity-shader(入门)
TCP三次握手、四次挥手