8

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 -->

1
  • 4
    In that case I believe you'll have to override the style. Like .container .box {background:initial} Commented Mar 24, 2017 at 17:14

4 Answers 4

5

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 -->

Sign up to request clarification or add additional context in comments.

Comments

1

You can apply a :not() selector to the child that includes a condition regarding the parent:

div.box:not(.container *) {
   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 -->

Taking from the Mozilla docs:

:not() is the negation pseudo-class that selects elements that match none of the specified selectors. So you use :not() to apply style to elements excluded by the specified selectors.

The key is, it takes any valid CSS selector, not just ones with attributes only referring to the final child element. In out case that's div.container *, which means "any element inside one with class container". So since that is in a :not(), it will exclude those.

Comments

0

In a way, the CSS rule you are asking for is sort of backwards. You should start with the most generic rules, and then add more specific ones. In your case, you should do something like the following:

/* Generic Box styles */
.box
{
  border: 1px solid black;
}

/* Boxes in a container */
.container .box
{
  color: blue;
}
<div class="box">Generic Box</div>
<div class="container">
  <div class="box">I'm in a container</div>
</div>

1 Comment

I know, but in my case (which is a bit more complex) that produces a lot of exclude CSS selectors. So i wanted to make it more simple.
0

Select all div.box or all div not inside .container? What you ask for and what you say you want selected in the html code sample are not the same thing. That said, your css selectors are just out of order. Try:

div.box:not(.container) {
   background:red;
}

and

<div class="box">box</div>
<div class="container">
    <div>txt</div>
    <div><div class="box">box</div></div>
</div>
<div class="box">box</div>

If you want all the divs, just remove the .box

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.