Khang
Khang

Reputation: 151

Alternating row colors except for duplicates

I've got a sticky situation I need some help with:

I need help coming up with a rule to style alternating background colors, except if there are duplicates of an element, in which case those need to be the same color, without affecting the subsequent elements.

Here's a preview of what that looks like

<section> a </section>               
<section> b </section>               
<section class="dupe"> c1 </section> 
<section class="dupe"> c2 </section>
<section class="dupe"> cn </section> 
<section> d </section>               
<section> e </section>

Here's what we're given:

Some researching suggested that using the general sibling ~ selector might help, but I could only use that to solve half of the problem, which is making them all the same color.

  section {
    background-color: blue;
  }
  section:nth-of-type(even) {
    background-color: red;
  }
  section:nth-of-type(even) ~ .dupe {
    background-color: red;
  }
  section:nth-of-type(odd) ~ .dupe {
    background-color: blue;
  }
<section> a </section>               
<section> b </section>               
<section class="dupe"> c1 </section> 
<section class="dupe"> c2 </section>
<section class="dupe"> c3 </section>
<section class="dupe"> cn </section> 
<section> d </section>               
<section> e </section>
    
d should be red but it's not, because it's 7th (odd)

Any suggestion would be highly appreciated.

Thanks in advance!

Upvotes: 2

Views: 106

Answers (1)

Y.T.
Y.T.

Reputation: 2729

The main logic with this solution is to check whether the first dupe element is followed after an odd element or an even element with a combination of :not() and the general sibling selector ~. Brief explanations can be found in the comments.

section:nth-of-type(even) {
  background-color: red;
}
section:nth-of-type(odd) {
  background-color: blue;
}
.dupe ~ section:nth-of-type(even) {                      
  /* elements after .dupe */
  background-color: blue;
}
.dupe ~ section:nth-of-type(odd) {                       
  /* elements after .dupe */
  background-color: red;
}
section:nth-of-type(even)+.dupe:not(.dupe ~ .dupe) {     
  /* style first .dupe by seeing whether it is immediately next to an even section */
  background-color: blue;
}
section:nth-of-type(even)+.dupe:not(.dupe ~ .dupe) ~ .dupe {
  /* style subsequent .dupe by seeing whether the first .dupe is immediately next to an even section */
  background-color: blue;
}
section:nth-of-type(odd)+.dupe:not(.dupe ~ .dupe) {
  /* style first .dupe by seeing whether it is immediately next to an even section */
  background-color: red;
}
section:nth-of-type(odd)+.dupe:not(.dupe ~ .dupe) ~ .dupe {
  /* style subsequent .dupe by seeing whether the first .dupe is immediately next to an even section */
  background-color: red;
}
<section> a </section>               
<section> b </section>    
<section class="dupe"> c1 </section> 
<section class="dupe"> c2 </section>
<section class="dupe"> c3 </section>
<section class="dupe"> cn </section> 
<section> d </section>               
<section> e </section>

section:nth-of-type(even) {
  background-color: red;
}
section:nth-of-type(odd) {
  background-color: blue;
}
.dupe ~ section:nth-of-type(even) {                      
  /* elements after .dupe */
  background-color: blue;
}
.dupe ~ section:nth-of-type(odd) {                       
  /* elements after .dupe */
  background-color: red;
}
section:nth-of-type(even)+.dupe:not(.dupe ~ .dupe) {     
  /* style first .dupe by seeing whether it is immediately next to an even section */
  background-color: blue;
}
section:nth-of-type(even)+.dupe:not(.dupe ~ .dupe) ~ .dupe {
  /* style subsequent .dupe by seeing whether the first .dupe is immediately next to an even section */
  background-color: blue;
}
section:nth-of-type(odd)+.dupe:not(.dupe ~ .dupe) {
  /* style first .dupe by seeing whether it is immediately next to an odd section */
  background-color: red;
}
section:nth-of-type(odd)+.dupe:not(.dupe ~ .dupe) ~ .dupe {
  /* style subsequent .dupe by seeing whether the first .dupe is immediately next to an odd section */
  background-color: red;
}
<section> a </section>               
<section> b </section>               
<section> c </section>
<section class="dupe"> d1 </section> 
<section class="dupe"> d2 </section>
<section class="dupe"> d3 </section>
<section class="dupe"> dn </section> 
<section> e </section>               
<section> f </section>       
<section> g </section>

Upvotes: 1

Related Questions