我想将 .special class 选择器应用于三个 h2 元素。
.special {
font-style: italic;
}
<h2 class="special">Help Please</h2>
<h2>Can we apply from one CSS rule to many h2 elements?</h2>
<h2>You help will be appreciated</h2>
是的,只需将类属性添加到所有三个
.special {
font-style: italic;
}
<h2 class="special">Help Please</h2>
<h2 class="special">Can we apply from one CSS rule to many h2 elements?</h2>
<h2 class="special">You help will be appreciated</h2>
或者,要选择父元素中的前三个h2
元素,请使用:
h2:nth-of-type(-n+3){
font-style: italic;
}
或者,要设置所有h2
元素的样式,请使用:
h2{
font-style: italic;
}
你可以这样尝试: 演示
.HTML:
<div class="special">
<h2>Help Please</h2>
<h2>Can we apply from one CSS rule to many h2 elements?</h2>
<h2>You help will be appreciated</h2>
</div>
<hr/>
<h2>Help Please normal h2</h2>
.CSS:
.special h2 {
font-style: italic;
}