什么是%和em长度值之间的区别?如果包含元素的字体大小为10px,则1em = 10px,100%也是10px。Stackoverflow中的答案均未回答此特定问题。
em
单位取决于font-size
。
它可以用于适应大小改变/缩放的文本。
在下面的代码片段中,您可以看到差异:
let list = document.querySelectorAll('.fs');
let fs = 10;
setInterval(() => {
if (fs > 36) fs = 10;
list.forEach(el => el.style.fontSize = fs+'pt');
fs += 1;
}, 100)
.em {
width: 2em;
background-color: cyan;
}
.perc {
width: 10%;
background-color: pink;
}
<div class="em fs">2em</div>
<div class="perc fs">10%</div>
position:relative/absolute
容器大小上。在我的代码片段中,它是 document.body
的宽度。 - vp_arthem
是相对于所讨论元素的当前字体大小的一种度量。1个em等于所讨论元素的当前字体大小。%
不依赖于任何特定属性。它的参考随着您在不同属性上应用它而改变。section{
width:100%;
}
div{
width:50px;
height:50px;
margin:0;
padding:0;
}
.a{
background-color:red;
}
.b{
background-color:blue;
width:2em;
}
.c{
background-color:green;
width:50%;
}
<section>
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
</section>
100%=full size it can extend
,所以它的大小可以延伸到父元素的大小。 - ab29007C
的父元素是覆盖整个屏幕的区域。因此,如果屏幕宽度为800像素,则 C
的宽度将为400像素。 - ab290072em
是它字体大小的两倍,而 200%
是它父元素大小的两倍。 - ab29007