I am trying to create a html layout that will be based of many adjacent sections. I want to have, somewhere between them, some divs that would control their appearance.
My html would be something like this:
<section class="section">White</section>
<section class="section">White</section>
<div class="color-scheme--violet-red"></div>
<section class="section">Colored</section>
<section class="section">Colored</section>
<div class="color-scheme--clear"></div>
<section class="section">White</section>
<section class="section">White</section>
<div class="color-scheme--violet-red"></div>
<section class="section">Colored</section>
<section class="section">Colored</section>
And I would want that every section that came AFTER a color-scheme div had a background defined by it, as I was trying with this scss:
$white: #fefefe;
$red: #c54839;
$violet: #6c195e;
.section {
height: 15em;
background: $white;
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
color: $red;
}
.color-scheme--violet-red ~ .section {
background-image: linear-gradient(to right, darken($violet, 10%) 0%, darken($red, 10%) 100%);
color: $white;
}
.color-scheme--clear ~ .section {
background: $white;
color: $red;
}
Here's a Fiddle with an working example: https://jsfiddle.net/raphaelaleixo/4featqrg/ (The last two sections are white, but they should be colored)
The thing is, as the last sections are both adjacent to the color-scheme--clear div AND to the color-scheme--violet-red, the one that is below in the css file gains priority, and overwrite the other.
My question is: Is there anyway to - using only html/css - to select every element that is the CLOSEST adjacent to another?
For this html, I can't nest this sections inside the "color controller" divs, neither I will be sure of how many divs will be until the next "controller" div (If this was know, I could nest multiple "next siblings" selectors and call it a day).
I am very opened to any css hacks that could achieve this layout :-)
Thanks!