Consider the following code:
<h2>
Working example
</h2>
<div class="test">
<button>FirstChild (Yellow)</button>
<button>Middle</button>
<button>Middle</button>
<button>LastChild (Red)</button>
</div>
<div class="test">
<h2>
Not working example - no DIVs
</h2>
<button>FirstChild (Yellow)</button>
<button>Middle</button>
<button>Middle</button>
<button>LastChild (Red)</button>
</div>
<div class="test">
<h2>
Not working example - Divs
</h2>
<div>
<button>FirstChild (Yellow)</button>
</div>
<div>
<button>Middle</button>
</div>
<div>
<button>Middle</button>
</div>
<div>
<button>LastChild (Red)</button>
</div>
</div>
And the following CSS:
.test button:first-child {
background: yellow;
}
.test button:last-child {
background: red;
}
.test button:not(:last-child):not(:first-child) {
background: cyan;
}
The result:
Why my CSS if not finding the child button element if it is inside a div or has another element (h2) in the middle ? There are no other button elements in the test div.
How to make this CSS consider the 3 given situations with the correct behaviour (Working example) ?