Html CSS 为特定类设置 h2 到 h6 的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7468891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:41:24  来源:igfitidea点击:

Css set a property for h2 to h6 for a specific class

htmlcss

提问by GibboK

I have a div, inside different <h>tags.

我有一个div, 在不同的<h>标签内。

I would like to apply some formatting to ALL <h>tags inside specifically to any with class cnt ONLY.

我想对<h>内部的所有标签应用一些格式,专门用于任何类 cnt ONLY

At the moment I'm using the following CSS without success... Any ideas what I'm doing wrong and how to fix it?

目前我正在使用以下 CSS 没有成功......任何想法我做错了什么以及如何解决它?

<div class="cnt">
    <h1>Some Text.</h1>
    <h2>Some Text.</h2>
    <h3>Some Text.</h3>
    <h4>Some Text.</h4>
    <h5>Some Text.</h5>
    <h6>Some Text.</h6>
</div>


    h2 h3 h4 h5 h6 .cnt 
    {
        font-size:16px;
        font-weight:700;
    }

回答by Ross

.cnt h2, .cnt h3, .cnt h4, .cnt h5, .cnt h6 {
    font-size:16px;
    font-weight:700;
}

read up on css specificity

阅读CSS 特异性

回答by captainclam

.cnt h1, .cnt h2, .cnt h3, .cnt h4, .cnt h5, .cnt h6 {
  font-size:16px;
  font-weight:700;
}

回答by Ash Clarke

captainclam has the right answer, but just so you understand a little about why it didn't work, the following selector:

船长蛤有正确的答案,但只是为了让您了解为什么它不起作用,以下选择器:

h2 h3 h4 h5 h6 .cnt

would represent something like the following:

将代表如下内容:

<h2><h3><h4><h5><h6><div class="cnt"></div></h6></h5></h4></h3></h2>

(anything with class cnt could be put in h6 for it to apply)

(任何类 cnt 都可以放在 h6 中以便应用)

@Ross, it had nothing to do with specificity; The selector was wrong.

@Ross,它与特异性无关;选择器错了。

回答by Barry Kaye

Try this:

尝试这个:

div.cnt h1, div.cnt h2, div.cnt h3, div.cnt h4, div.cnt h5, div.cnt h6 
{
    /* ... */
}