I'm trying to always select the first element with the class ".chartKeyListItem". It is under the parent "chartKey". So this works great with the first-child selector if it is the first element but sometimes I have 3 items before it so I'd have to use the nth-selector(4) this works for that instance. But my code should be able to handle both without breaking eachother. (Which it does if both CSS classes are loaded in because for the first instance it targets the 1st and 4th "chartKeyListItem") Is there a way to just only select the first ".chartKeyListItem"?
Also, the amount of chartKeyListItem divs is not definite and can be anywhere from 2 to 20 and the MinMax labels will only show up for some pages but the CSS needs to be able to handle when they're are there and when they are not...
Code below: Instance 1:
<div class='chartKey'>"
<div class='chartKeyListItem'> //Just want to add margin to this one
<div class='chartKeyListItem'>
<div class='chartKeyListItem'>
<div class='chartKeyListItem'> //THIS ONE GETS BOTH CLASSES WHICH I DONT WANT
</div>
Instance 2:
<div class='chartKey'>"
<div class='pollResultsMinMaxLabel' style='width:auto;float:left;margin-left:20px;'>Min</div>
<div class='pollResultsMinMaxLabel' style='width:auto;float:left;margin-left:20px;'>Max</div>
<br/>
<div class='chartKeyListItem'> //Just want to add margin to this one
<div class='chartKeyListItem'>
<div class='chartKeyListItem'>
<div class='chartKeyListItem'>
<div class='chartKeyListItem'> //This one will get both class too
</div>
CSS:
.chartKeyListItem:first-child{
margin-left: 60px !important;
}
.chartKeyListItem:nth-child(4) {
margin-left: 60px !important;
}