【问题标题】:a div has no inherited font size from bodydiv 没有从 body 继承的字体大小
【发布时间】:2018-10-05 14:20:22
【问题描述】:

body{
font-size:0.9em;
}

.m1{
font-size:0.9em;
}
<div class='navt'>
<div class='m1'>LOREM</div>
<div class='m1'>LOREM</div>
</div>

如果您从m1 中删除font-size,您会看到字体大小发生了变化,即没有从body 继承。

我希望body 中的所有 div 都有一个 font-size 等于为 body 声明的那个。

难道我需要为每个div单独设置font-size

【问题讨论】:

    标签: html css font-size


    【解决方案1】:

    当使用em 时,这意味着大小是相对于其父级的。您应该使用rem 使其相对于文档的根目录。

    在这种情况下,.m1 的字体大小为 0.9 * 0.9 = 0.81px。

    body  {
      font-size: 0.9em;
    }
    
    .m1 {
      font-size: 0.9em;
    }
    
    .m2 {}
    
    .m3 {
      font-size: 0.9rem;
    }
    
    .m4 {
      font-size: 0.9rem;
    }
    <div class='navt'>
      <div class='m1'>LOREM</div>
      <div class='m2'>LOREM</div>
      <div class='m3'>LOREM</div>
      <div class='m4'>LOREM</div>
    </div>

    【讨论】:

    • @puerto 我的荣幸
    【解决方案2】:

    body { 字体大小:1em} .text-one、.text-2、.text-3 {font-size: 1em}

    你好,再见

    【讨论】:

    • 请简单介绍一下您的代码的作用。
    • 点评来源: 欢迎来到 Stack Overflow!请不要只用源代码回答。尝试对您的解决方案如何工作提供一个很好的描述。请参阅:How do I write a good answer?。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 2014-07-31
    相关资源
    最近更新 更多