ed quijano
ed quijano

Reputation: 11

applying class selector to many h2 elements

I want to apply .special class selector to a three h2 elements.

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

Upvotes: 0

Views: 1197

Answers (2)

G.L.P
G.L.P

Reputation: 7217

You can try like this: 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;
}

Upvotes: 0

SW4
SW4

Reputation: 71170

Yes, simply add the class attribute to all three

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

Alternatively, to select the first three h2 elements within a parent, use:

h2:nth-of-type(-n+3){
    font-style: italic;
} 

Or, to style all h2 elements, use:

h2{
    font-style: italic;
} 

Upvotes: 2

Related Questions