I am looking to alternate the background color of divs between odd and even, with the last div being grey and the second to last div being green. I have tried using the odd/even classes in CSS, but it did not work as expected.
.main{
width:500px;
height: 500px;
border:1px solid blue;
}
.box{
width:300px;
height: 100px;
border:1px solid red;
}
.box:nth-child(odd) {
background: grey;
}
.box:nth-child(even) {
background:green;
}
<div class="main">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
</div>
I could use some guidance on achieving my desired color scheme for the divs. Any help would be appreciated. Thank you.