简体   繁体   English

CSS中的“Adjacent Until ...”是否有选择器(或任何方式)?

[英]Is there a selector (or any way around) for “Adjacent Until…” in CSS?

I am trying to create a html layout that will be based of many adjacent sections. 我正在尝试创建一个基于许多相邻部分的html布局。 I want to have, somewhere between them, some divs that would control their appearance. 我想在它们之间的某个地方有一些可以控制它们外观的divs

My html would be something like this: 我的HTML会是这样的:

<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: 并且我希望在color-scheme div之后出现的每个section都有一个由它定义的背景,正如我尝试使用这个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) 这是一个工作示例的小提琴: https//jsfiddle.net/raphaelaleixo/4featqrg/ (最后两个部分是白色的,但它们应该是彩色的)

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. 问题是,因为最后一部分都与color-scheme--clear相邻color-scheme--clear divcolor-scheme--violet-red ,css文件中的下一个获得优先权,并覆盖另一个。

My question is: Is there anyway to - using only html/css - to select every element that is the CLOSEST adjacent to another? 我的问题是: 无论如何 - 仅使用html / css - 选择与另一个相邻的CLOSEST的每个元素?

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). 对于这个html,我不能将这些部分嵌套在“颜色控制器”div中,我也不确定在下一个“控制器”div之前将有多少div(如果知道的话,我可以嵌套多个“下一个兄弟姐妹” “选择者,并称它为一天)。

I am very opened to any css hacks that could achieve this layout :-) 我对任何可以实现这种布局的css hacks 非常开放:-)

Thanks! 谢谢!

One hacky idea is to increase the specifity of each selector consider the previous color-sheme before it. 一个愚蠢的想法是增加每个选择器的特定性,考虑它之前的前一个color-sheme So the more color-sheme we have before the more the selector will be specific thus it will win the one before it. 因此,在选择器具体化之前我们拥有的color-sheme越多,因此它将赢得之前的选择。

Of course, you will need to write many CSS rules depending on how many section you will have. 当然,您需要编写许多CSS规则,具体取决于您将拥有多少部分。 You can consider SASS/LESS to easily generate the code: 你可以考虑SASS / LESS来轻松生成代码:

 .section { height: 15em; background: white; font-family: sans-serif; display: flex; justify-content: center; align-items: center; color: red; } .color-scheme--violet-red ~ .section, [class*="color-scheme" ] ~ .color-scheme--violet-red ~ .section, [class*="color-scheme" ] ~ [class*="color-scheme" ] ~ .color-scheme--violet-red ~ .section, [class*="color-scheme" ] ~ [class*="color-scheme" ] ~ [class*="color-scheme" ] ~ .color-scheme--violet-red ~ .section, [class*="color-scheme" ] ~ [class*="color-scheme" ] ~ [class*="color-scheme" ] ~ [class*="color-scheme" ] ~ .color-scheme--violet-red ~ .section{ background-image: linear-gradient(to right, purple 0%, red 100%); color: white; } .color-scheme--clear ~ .section, [class*="color-scheme" ] ~ .color-scheme--clear ~ .section, [class*="color-scheme" ] ~ [class*="color-scheme" ] ~.color-scheme--clear ~ .section, [class*="color-scheme" ] ~ [class*="color-scheme" ] ~ [class*="color-scheme" ] ~.color-scheme--clear ~ .section, [class*="color-scheme" ] ~ [class*="color-scheme" ] ~ [class*="color-scheme" ] ~ [class*="color-scheme" ] ~.color-scheme--clear ~ .section{ background: white; color: red; } 
 <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> <div class="color-scheme--clear"></div> <section class="section">White</section> <section class="section">White</section> <section class="section">White</section> <section class="section">White</section> <div class="color-scheme--violet-red"></div> <section class="section">White</section> <section class="section">White</section> 

This would be another hacky solution. 这将是另一个hacky解决方案。 Adding a + selector for each .section . 为每个.section添加一个+选择器。

Assuming there is a max number of .sections in each 'colour' - you could do something like below (see demo) 假设每个'颜色'中有最大数量的.sections - 你可以做类似下面的事情(见演示)

The difficulty is (obviously) that you would need to write as many + .section as there are possible siblings of the same colour in a given section 困难(显然)你需要编写尽可能多的+ .section因为在给定的部分中可能存在相同颜色的兄弟姐妹

Of course, you could use SASS/LESS to generate the code for you but you still would need to find out how many to generate. 当然,你可以使用SASS / LESS为你生成代码,但是你仍然需要知道要生成多少代码。

 .section { height: 5em; background: white; font-family: sans-serif; display: flex; justify-content: center; align-items: center; color: red; } /* up to 4 coloured sections */ .color-scheme--violet-red + .section, .color-scheme--violet-red + .section + .section, .color-scheme--violet-red + .section + .section + .section, .color-scheme--violet-red + .section + .section + .section + .section { background-image: linear-gradient(to right, purple 0%, red 100%); color: white; } .color-scheme--clear + .section { background: white; color: red; } 
 <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> <section class="section">Colored</section> <div class="color-scheme--clear"></div> <section class="section">White</section> <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> <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> <section class="section">White</section> <section class="section">White</section> <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM