Even without any HTML structure provided, I have created two simple examples:
A. Using the :first-child
or :nth-child
selector only works if there are four divs with the same class as immediate children of the same container and no other elements inside that container.
Here's a code snippet for reference:
#wrapper {
background:blue;
}
.formErrorContent{
height:100px;
background:red;
width:100px;
}
.formErrorContent:first-child {
background:green;
}
.formErrorContent:nth-child(2) {
background:yellow;
}
.formErrorContent:nth-child(3) {
background:white;
}
.formErrorContent:last-child {
background:black;
}
<div id="wrapper">
<div class="formErrorContent">
</div>
<div class="formErrorContent">
</div>
<div class="formErrorContent">
</div>
<div class="formErrorContent">
</div>
</div>
B. If there are additional elements in the container, you can target the first div with the specified class based on its preceding element (remember CSS cascades from top to bottom).
For instance, when there is a p
element before .formErrorContent
, you should use the sibling selector +</
like this:
p + .formErrorContent { background: green; }
Here's an example snippet:
#wrapper {
background: blue;
}
p {
color: white;
}
.formErrorContent{
height: 100px;
background: red;
width: 100px;
}
p + .formErrorContent {
background: green;
}
<div id="wrapper">
<p>
This is a p element that precedes the other divs in this container.
</p>
<div class="formErrorContent">
</div>
<div class="formErrorContent">
</div>
<div class="formErrorContent">
</div>
<div class="formErrorContent">
</div>
</div>
The key takeaway is that there are multiple approaches to achieve your desired outcome, but it ultimately depends on the HTML structure at hand.