CSS类嵌套不起作用

时间:2018-08-01 01:49:34

标签: html css

我有一个CSS类,并且我想确保每当将h2标签分配给该类时,就会在原始类的顶部为h2标签提供附加的CSS。这是我目前在CSS和HTML上拥有的东西,但目前没有呈现出text-align类。

.aligncenter {
	clear: both;
	color: #000000
	h2{
		text-align: center;
	}
<div>
<h2 class="aligncenter"> H2 Text that should be black and centered</h2>
</div>

3 个答案:

答案 0 :(得分:1)

您不能使用常规CSS来做到这一点。用常规的CSS实现您想要的唯一方法是这样的:

.aligncenter {
    clear: both;
    color: #000000
}

h2.aligncenter {
    clear: both;
    color: #000000
    text-align: center;
}

要进行嵌套和其他有趣的工作,您需要一些CSS扩展语言,例如SASS

答案 1 :(得分:0)

.aligncenter {
	clear: both;
	color: #000000
	h2{
		text-align: center;
	}
<div>
<h2 class="aligncenter"> H2 Text that should be black and centered</h2>
</div>

答案 2 :(得分:0)

<style>
h2.aligncenter {
        clear: both;
        color: #000000;
        text-align: center;
        }
</style>

    <div><h2 class="aligncenter"> H2 Text that should be black and centered</h2></div>
 <!-- end snippet -->