简体   繁体   English

有没有办法 select 一个基于背景颜色的元素?

[英]Is there a way to select an element based on background color?

I want to change the background-color of a child of a div with a certain background color and was wondering if this could be done with CSS. I'll explain the case below.我想更改具有特定背景颜色的 div 的子项的背景颜色,并且想知道是否可以使用 CSS 来完成此操作。我将在下面解释这个案例。

something like:就像是:

.container[background-color=some color] .content {
    background-color: some other color
}

My guess is that it can't be done because you would then be able to do something along the lines of:我的猜测是它无法完成,因为这样你就可以按照以下方式做一些事情:

.div {
    background-color: some color
}

.div[background-color=some color] {
    background-color: some other color
}

Which would create some kind of circularity where the div would be selected, set to the other color, then not be selected anymore and fall back on the original definition, but then be selected again because it has that original color.这会产生某种循环,其中 div 将被选中,设置为另一种颜色,然后不再被选中并返回到原始定义,但随后再次被选中,因为它具有原始颜色。

I don't think the:has, :is and:where selectors work this way either but I was hoping there was some way this could be done while avoiding doing it in Javascript.我不认为 :has、:is 和:where 选择器也以这种方式工作,但我希望有一些方法可以做到这一点,同时避免在 Javascript 中这样做。

The Case案子
Divs are created dynamically based on errors in user input. Div 是根据用户输入的错误动态创建的。 These divs may or may not contain a certain type of error for which another div is needed这些 div 可能包含也可能不包含需要另一个 div 的某种类型的错误

// simple errors
<div class="errors">
    <p>Error 1</p>
    <p>Error 2</p>
    <p>Error 3</p>
    <p>Error 4</p>
</div>

// more complex errors
<div class="errors">
    <p>Error 1</p>
    <div class="complex-error">
        <p>Complex Error 1</p>
    </div>
</div>

I give the errors div a background color with odd/even我给错误 div 设置了奇数/偶数的背景颜色

.errors {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto;
    border-radius: 4px;
    margin: 8px 0px;
}

.errors:nth-child(odd) {
    background-color: #dee2e6;
}

.errors:nth-child(even) {
    background-color: #f8f9fa;
    border: 2px solid #dee2e6;
}

I was wondering whether or not the elements with class errors could be selected based on background color to then select it's child.我想知道是否可以根据背景颜色选择带有 class errors的元素,然后 select 是它的子元素。

I'll post my own solution as the answer.我会发布我自己的解决方案作为答案。

The best way to do it without using JavaScript would be with classes.不使用 JavaScript 的最佳方法是使用类。 You can add the first background-color with a class that you add to the children that need to have that background color.您可以添加带有 class 的第一个background-color ,您将其添加到需要具有该背景颜色的子级。 Then, you change the background-color of only the children that have that class. And if you need to you can add and remove the class dynamically with JavaScript (as you are not providing an example of the whole app I don't fully understand the behaviour you are looking for).然后,您仅更改具有该 class 的孩子的background-color 。如果需要,您可以使用 JavaScript 动态添加和删除 class(因为您没有提供整个应用程序的示例,我不完全理解您正在寻找的行为)。

div.black {
    background-color: black;
}

div.white {
    background-color: white;
}

div.black {
    background-color: #5A5A5A;
}

This is the way you could do it.这是你可以做到的方式。 Separate the class out to style the background color then use the class selector .class1.class2 to apply the rule to only the parent.将 class 分开以设置背景颜色的样式,然后使用class 选择器.class1.class2 将规则仅应用于父级。 Use the descendent class combinator to apply the background colour to the child.使用后代 class 组合器将背景颜色应用于子项。

Here's an example这是一个例子

 .parent { border: 1px solid black; padding: 1rem; font-size: 1.5rem; margin-top: 1rem; }.yellow-backgroud { background-color: yellow; }.red-background { background-color: red; } /* this selects all child divs where the parent has the parent class AND the yellow-background class */.parent.yellow-background div { background-color: green; } /* this selects all child divs where the parent has the parent class AND the red-background class */.parent.red-background div { background-color: orange; }
 <div class='parent yellow-background'> Parent <div> This is a the child of the yellow background parent </div> </div> <div class='parent red-background'> Parent <div> This is a the child of the red background parent </div> </div> <div class='parent'> Parent <div> This is a the child with no parent background color set. </div> </div>

The odd/even selector can already be used to select those elements.奇数/偶数选择器已经可以用于 select 这些元素。

.errors:nth-child(odd) {
    background-color: #dee2e6;
}

.errors:nth-child(odd) .complex-error {
    background-color: #f8f9fa;

}

.errors:nth-child(even) {
    background-color: #f8f9fa;
    border: 2px solid #dee2e6;
}

.errors:nth-child(even) .complex-error {
    background-color: #dee2e6;
}

There was no reason to select them after the fact when you can just select them at the place where they are defined.事后没有理由 select 它们,因为您可以在定义它们的地方只 select 它们。

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

相关问题 根据元素的属性更改背景颜色的更好方法 - Better way to change background color based on element's property 根据背景元素在元素上有不同的颜色 - Have different color on element based on background element 选择一个元素,它的背景颜色在jQuery中不起作用 - Select an element with it's background color is not working in jQuery 根据 OPTION 标签的背景在选择的 HTML 标签上设置背景颜色 - Set background color on select HTML tag based on the background of an OPTION tag 有没有办法根据 CSS 中另一个元素的背景颜色来更改文本的颜色? - is there a way to change the color of the text depending on the background color of another element in CSS? 如何使用 JavaScript 选择 div 元素并更改背景颜色? - How to select a div element with JavaScript and change background color? 根据php变量更改:before元素的背景颜色 - Changing the background color of a :before element based on a php variable 基于标题的jQuery更改下拉列表元素的背景颜色 - JQuery Change Background Color of Dropdown List Element Based on Title Javascript:基于选择选项设置背景颜色时出错 - Javascript: Error when setting background color based on select option 在CSS3或HTML5中更改元素背景颜色的有效方法 - Efficient way of changing background color of an element in CSS3 or HTML5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM