【问题标题】:Why font size is different in divs? [duplicate]为什么div中的字体大小不同? [复制]
【发布时间】:2019-09-25 22:04:29
【问题描述】:

我为所有 div 赋予了相同的字体大小(2em)。但是#left#right 中的字体大小显然更大。我不知道为什么浏览器会给出这个结果。

这是一个代码sn-p:

div {
  color: white;
  font-size: 2em;
}

#header {
  width: 100%;
  height: 120px;
  background: #99ff00;
}

#main {
  width: 100%;
  height: 400px;
  background: #99ff99;
}

#left {
  width: 30%;
  height: 100%;
  float: left;
  background: #999999;
}

#right {
  width: 70%;
  height: 100%;
  float: left;
  background: #553344;
}

#footer {
  clear: both;
  width: 100%;
  height: 80px;
  background: #66ff66;
}
<div id="header">header</div>
<div id="main">
  <div id="left">left</div>
  <div id="right">right</div>
</div>
<div id="footer">footer</div>

【问题讨论】:

  • em 是一个相对单位。 2em 表示父级 font-size 的两倍。因此,使用font-size: 2em 嵌套元素意味着您将每层的尺寸加倍。 CSS values and units
  • 建议改用rem

标签: css


【解决方案1】:

div 默认字体大小为 16px。

父容器&lt;div id="main"&gt; 具有属性font-size:2em;。表示font-size为32px(parent-container-font-size*2)。

#right 和 #left 块也是如此,它们具有属性font-size:2em;。也就是说font-size是64px(parent-container-font-size*2)。

【讨论】:

  • 一路下来都是乌龟
  • 我明白了,非常感谢。
  • 酷!不客气!
猜你喜欢
  • 1970-01-01
  • 2013-03-29
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多