2

I am trying to set every other color of a list using a Font Awesome icon as the bullet. However, instead of it styling every other bullet, it styles all the bullets. If you switch back and forth between (odd) and (even) :nth-child, you can see all the bullet colors changing when you run the script. Why does it do it this way? Is there a better way of going about this?

.fa-asterisk:before {
    content: "\f069";
    color: #77c4d3;
    vertical-align: middle;
    line-height: 1.6em;
}

.fa-asterisk:nth-child(even):before {
  color: #7d8a2e;
  }

ul.fa-ul {
  list-style: none;
  }
<ul class="fa-ul">
  <li><i class="fa-li fa fa-asterisk"></i>HGTV Living BIG Sky Realtor</li>
  <li><i class="fa-li fa fa-asterisk"></i>Member Bitterroot Valley Board of Realtors</li>
  <li><i class="fa-li fa fa-asterisk"></i>Montana Regional MLS</li>
  <li><i class="fa-li fa fa-asterisk"></i>National Association of Realtors</li>
  </ul>
Trisha
  • 539
  • 3
  • 10
  • 30

2 Answers2

4

You need to target the li elements with your odd/even selector, as .fa-asterisk is a child element and is only known to the DOM as the first child of each li.

.fa-asterisk:before {
    content: "\f069";
    color: #77c4d3;
    vertical-align: middle;
    line-height: 1.6em;
}

li:nth-child(even) .fa-asterisk:before {
  color: #7d8a2e;
  }

ul.fa-ul {
  list-style: none;
  }
<ul class="fa-ul">
  <li><i class="fa-li fa fa-asterisk"></i>HGTV Living BIG Sky Realtor</li>
  <li><i class="fa-li fa fa-asterisk"></i>Member Bitterroot Valley Board of Realtors</li>
  <li><i class="fa-li fa fa-asterisk"></i>Montana Regional MLS</li>
  <li><i class="fa-li fa fa-asterisk"></i>National Association of Realtors</li>
  </ul>
Jon Uleis
  • 17,693
  • 2
  • 33
  • 42
0

the asterisk is always the first child of the li. You need to apply nth-child() to the li tag. see below

.fa-asterisk:before {
    content: "\f069";
    color: #77c4d3;
    vertical-align: middle;
    line-height: 1.6em;
}

li:nth-child(even) .fa-asterisk:before {
  color: #7d8a2e;
  }

ul.fa-ul {
  list-style: none;
  }
<ul class="fa-ul">
  <li><i class="fa-li fa fa-asterisk"></i>HGTV Living BIG Sky Realtor</li>
  <li><i class="fa-li fa fa-asterisk"></i>Member Bitterroot Valley Board of Realtors</li>
  <li><i class="fa-li fa fa-asterisk"></i>Montana Regional MLS</li>
  <li><i class="fa-li fa fa-asterisk"></i>National Association of Realtors</li>
  </ul>
mcgraphix
  • 2,723
  • 1
  • 11
  • 15