Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Rule exclude parent class

Tags:

How can I write a CSS Rule that selects all div.box that are not inside .container?

The following snippet is not working because there is a div without .container inside the div.container.

div:not(.container) .box {
   background:red;
}
<div class="box">box</div> <!-- select this -->
<div class="container">
    <div>txt</div>
    <div><div class="box">box</div></div>
</div>
<div class="box">box</div> <!-- select this -->
like image 405
Mike Avatar asked Mar 24 '17 17:03

Mike


People also ask

Can you exclude a class in CSS?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

Can CSS be used to find child => parent page element?

There is currently no way to select the parent of an element in CSS in a way that works across all browsers.

Is there a CSS parent selector?

The element>element selector is used to select elements with a specific parent. Note: Elements that are not directly a child of the specified parent, are not selected.


1 Answers

If you do not want to override every attribute, the only way I see is to give an additional class to the boxes inside of the specific container.

.box:not(.exclude) {
  background: red;
}
<div class="box">box</div> <!-- select this -->
<div class="container">
    <div>txt</div>
    <div><div class="box exclude">box</div></div>
</div>
<div class="box">box</div> <!-- select this -->
like image 58
Marvin Avatar answered Sep 25 '22 10:09

Marvin