< h1>不是从它的容器继承字体权重 [英] <h1> is not inheriting font-weight from its container

查看:386
本文介绍了< h1>不是从它的容器继承字体权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 CSS 中有继承问题。我做了一个 div 容器放置一个 h1 元素:

 < body> 
< div class =container>
< h1>测试< / h1>
< / div>
< / body>

根据我的理解, h1 元素是粗体并且具有2em的字体权重。为了抵消这种情况,我在容器上放置了以下 CSS 属性。

  .container {
font-size:5em;
font-weight:normal;
}

h1 继承两个属性和 h1 font-size 更改为 5em 。但< h1> 的字体重量保持粗体,即使它继承了 font-weight:normal; 其父元素。这是我在我的屏幕上看到的。





为什么 h1 的font-weight不会更改为 font-weight:normal; ?我认为它是因为有一些CSS属性,像font-weight,一个子元素不从其父继承。



  .container {font-size:5em; font-weight:normal;}  

 < body& < div class =container> < h1>测试< / h1> < / div>< / body>  

解决方案

  .container {font-size:8px;}  

 < div class =container>我是8px文字< h1>这是H1在16px font-size(8px * 2em = 16px)< / h1>< / div>  

div>



h1 既不继承 font-size font-weight ,因为隐式继承仅适用于未定义的继承的属性。

你看到的是基于5em的2em(如果容器具有 font-size:1em; ),则等于10em。



font-size font-weight



作为参考:




I have a problem with inheritance in CSS. I made a div container put an h1 element within it as such:

<body>
   <div class="container">
     <h1> test </h1>
   </div>
</body>

From what I understand, all text within the h1 element is bold and has a font-weight of 2em. To counteract this, I placed the following CSS properties on the container.

.container{
     font-size:5em;
     font-weight:normal;
}

h1 seems to inherit both properties and h1's font-size changes to 5em as expected. But <h1>'s font-weight stays bold, even though it inherited font-weight:normal; from its parent element. Here's what I see on my screen.

Why does the h1's font-weight not change to font-weight:normal;? I'm think its because there are certain CSS properties, like font-weight, that a child element does not inherit from its parent.

.container{
     font-size:5em;
     font-weight:normal;
}

<body>
   <div class="container">
     <h1> test </h1>
   </div>
</body>

解决方案

.container {
  font-size: 8px;
}

<div class="container">
    I'm 8px text
    <h1>This is H1 in 16px font-size (8px * 2em = 16px)</h1>
</div>

h1 neither inherits font-size nor font-weight because implicit inheritance only applies to inherited properties not defined otherwise.

What you are seeing is 2em based on 5em (equally large as 10em if container had font-size: 1em;).

Both font-size and font-weight have other property values defined in the user agent stylesheet as your dev tools are showing you.

For reference:

这篇关于&lt; h1&gt;不是从它的容器继承字体权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆