Is there a way to select the nth child of type and apply different colors without assigning IDs because they are dynamically generated in a loop from a template engine?
I found some useful information on this topic here:
https://www.w3schools.com/cssref/sel_first-of-type.asp
https://www.w3schools.com/cssref/sel_nth-of-type.asp
https://www.w3schools.com/cssref/sel_last-of-type.asp
After implementing the code snippet below, I noticed that all headers have the same color. Can anyone explain why they don't have different colors as expected?
#service {
background: #000000;
color: #ffffff;
border-bottom: 1px solid #424242;
}
#serviceItems {
display: flex;
margin: 0 50px;
}
.serviceItem {
flex: 1;
text-align: center;
margin-top: 0;
}
.serviceItemHeader:first-of-type {
color: #80E000;
}
.serviceItemHeader:nth-of-type(2) {
color: #F97A00;
}
.serviceItemHeader:nth-of-type(3) {
color: #B277DD;
}
.serviceItemHeader:last-of-type {
color: #e2c100;
}
<div id="service" class="section">
<div id="serviceItems">
<div class="serviceItem">
<h4 class="serviceItemHeader">Container 1</h4>
<h4>Container 1</h4>
</div>
<div class="serviceItem">
<h4 class="serviceItemHeader">Container 2</h4>
<h4>Container 2</h4>
</div>
<div class="serviceItem">
<h4 class="serviceItemHeader">Container 3</h4>
<h4>Container 3</h4>
</div>
<div class="serviceItem">
<h4 class="serviceItemHeader">Container 4</h4>
<h4>Container 4</h4>
</div>
</div>
</div>