1

I want apply green color for the first nth child flag

<div id="axis">
     <div class="super"></div>
     <div class="flag">flag 1</div><!-- I want this text to be green-->
     <div class="super"></div>
     <div class="flag">flag 2</div>
     <div class="super"></div>
     <div class="flag">flag3</div>
     <div class="super"></div>
</div>

Css:

#axis{
    color:red;
}

#axis .flag:nth-of-type(1){
    color:green;  
}

#axis .flag:nth-child(1){
    color:green;
}

I tried in both scenario but not working...

Fiddle

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49

2 Answers2

0

Another way to acheive this is

<div id="axis">
    <div class="super"></div>
    <div class="flag">flag 1</div><!-- I want this text to be green-->
    <div class="super"></div>
    <div class="flag">flag 2</div>
    <div class="super"></div>
    <div class="flag">flag3</div>
    <div class="super"></div>
</div>

In CSS it will be

#axis > div:nth-child(2) {
    color: green;
}

Another way:

Apply a common CSS:

.flag {
   /* first .flag element styles here */
   /* though these styles are applied to all .flag elements, in later step they get overridden. */
}

.flag + .flag{
    /* override above styles */
    /* Applies css to all the .flag elements except the first one */
}
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • your answer is static. Suppose nth-child(2) class is super means it also apply green it not the scenario – Sudharsan S Sep 18 '14 at 09:22
  • @JqueryKing Since, this post was closed.. I added my answer as "Another way" in this post. Abhijeet, I hope you are not offended. – Mr_Green Sep 18 '14 at 09:25
  • @Mr_Green Not at all, I learned something so why should I be offended. – Abhijeet yadav Sep 18 '14 at 09:29
  • @Mr_Green - However, your way doesn't work. You want the general sibling combinator `~`, not the adjacent sibling combinator `+`. – Alohci Sep 18 '14 at 09:55
  • @Alohci both works same. because I am not using `:first-child` or and other specific pseudo class here. – Mr_Green Sep 18 '14 at 09:56
0

Another way DEMO

<ul id="axis">
    <li class="flag">flag 1</li>
    <li class="flag">flag 2</li>
    <li class="flag">flag3</li>
</ul>

.flag {
    list-style:none;
    color:red;
}


.flag:nth-of-type(1) {

    color:green;  
}
Alex Wilson
  • 2,421
  • 11
  • 13