Problem:
I want to select all the red boxes that are consecutive and change its background color to yellow. But css applied is not working.
Expected result:
2nd red box should turn to yellow.
What I have tried:
I have tried to use +
operator to select elements that are consecutive to each other, but it does not give me expected results.
This is the exact code that I use to select consecutive elements that are 1 level deep:
ul li > div.red + ul li > div.red {
background: yellow;
}
My code:
Here is the code sandbox: https://codesandbox.io/s/divine-wave-1bjmp?file=/main.css
ul {
list-style-type: none;
}
ul li>div {
height: 100px;
width: 100px;
border: 1px solid black;
margin: 20px;
}
ul li>div.red {
background: red;
}
ul li>div.green {
background: green;
}
ul li>div.red+ul li>div.red {
background: yellow;
}
<ul>
<li>
<div class="red" />
</li>
<li>
<div class="red" />
</li>
<li>
<div class="green" />
</li>
<li>
<div class="red" />
</li>
</ul>
Notes:
I am not allowed to make any changes to HTML. This problem should be solved using pure css.