将类选择器应用于许多h2元素

时间:2015-04-28 07:27:00

标签: css

我想将.special类选择器应用于三个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>  

2 个答案:

答案 0 :(得分:2)

是的,只需将class属性添加到所有三个

.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;
} 

答案 1 :(得分:0)

您可以尝试这样做: Demo

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;
}