What is the best way to ensure that the :not() CSS exclusion works reliably in all scenarios?

I'm attempting to exclude certain CSS rules when a specific CSS class is defined as a parent using the following rule:

.cc div:not(.cc-ei) li { color: red; }

However, this rule does not work in all cases, as evident in the code snippet below and the accompanying Codepen demo:

.item {
  color: green;
}

.cc div:not(.cc-ei) li {
  color: red;
}
<div class="cc">
  <div>
    <ul>
      <li>Should be red</li>
    </ul>
  </div>

  <div class="cc-ei">
    <ul>
      <li class="item">Should be green</li>
    </ul>
  </div>

  <div class="toto">
    <div class="cc-ei">
      <ul>
        <li class="item">Should be green</li>
      </ul>
    </div>
  </div>
</div>

Answer №1

To easily meet your needs, you can utilize the following CSS rule to style the <li> elements:

li:not(.cc-ei li) {
  color: red;
}

Below is the updated code with comments for better understanding:

body {
  font: 700 16px/1.5 Arial;
}

.item {
  color: green;
}

/*
  This targets all <li> elements that do not have the class
  "cc-ei", ensuring only specific ones are styled in red:
*/
li:not(.cc-ei li) {
  color: red;
}
<div class="cc">
  <div>
    <ul>
      <li>Should be red</li>
      <li>Should be red</li>
      <li>Should be red</li>
    </ul>
  </div>

  <div class="cc-ei">
    <ul>
      <li class="item">Should be green</li>
      <li class="item">Should be green</li>
      <li class="item">Should be green</li>
    </ul>
  </div>

  <div class="toto">
    <div class="cc-ei">
      <ul>
        <li class="item">Should be green</li>
        <li class="item">Should be green</li>
        <li class="item">Should be green</li>
      </ul>
    </div>
  </div>
</div>

Resources:

Answer №2

From a logical standpoint, your selector is searching for list items within any div that does not have the specified class. The problem arises when your list is nested in multiple divs, one of which lacks the class, causing this condition to take precedence. To resolve this issue, you can specify a child relationship (rather than a general descendant relationship) to ensure it functions correctly.

.item {
  color: green;
}

.cc div:not(.cc-ei)>ul li {
  color: red;
}
<div class="cc">
  <div>
    <ul>
      <li>Should be red</li>
    </ul>
  </div>

  <div class="cc-ei">
    <ul>
      <li class="item">Should be green</li>
    </ul>
  </div>

  <div class="toto">
    <div class="cc-ei">
      <ul>
        <li class="item">Should be green</li>
      </ul>
    </div>
  </div>
</div>

Answer №3

It's as easy as pie (or should I say, as easy as the author - that's me). Just specify your desire: make li items inside .cc-ei turn green.

.item {
  color: green;
}

.cc li {
  color: red;
}

.cc-ei li {
  color: green;
}
<div class="cc">
  <div>
    <ul>
      <li>Should be red</l;'>
    </ul>
  </div>

  <div class="cc-ei">
    <ul>
      <li class="item">Should be green</li>
    </ul>
  </div>

  <div class="toto">
    <div class="cc-ei">
      <ul>
        <li class="item">Should be green</li>
      </ul>
    </div>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Problem with escaping special characters in random string HTML

As I was in the process of creating a JavaScript tool to generate random strings, with or without special characters, I stumbled upon an inspiring snippet that caught my attention: (): function randStr(len) { let s = ''; while (len--) s += ...

Choose a unique text color

Having trouble with my HTML select form and modifying text color for specific options. In the provided example, I changed the color for the Tuesday option, but it only shows up when scrolling through the options. How can I make the color change visible for ...

Unique styling for underlines in pop-up with custom CSS

When exploring my codesandbox project, I encountered 2 questions: How can I create a custom underline CSS similar to the UI image below the file name and trash icon? I tried using text-decoration:none, but it doesn't match the UI. How do I center a U ...

The CSS provider for Gtk+3 is malfunctioning

I've been attempting to set an image as the background of a GtkBox using a CSS provider, and also customize the style of some label text, but no matter what I try, nothing seems to work. Below is the content of my custom.css file: GtkBox#Home_Princi ...

Guide to adding a period symbol using HTML5 speech recognition technology

I have been implementing the HTML5 speech recognition feature on my website with success. Previously, when I would say "full stop," it would display "." as expected. However, over the past four days, it has been transcribing the words "full stop" instead o ...

Would Django be the right fit for the project I have in mind?

Although I have experience with HTML/CSS/Javascript and Python individually, I find myself at a loss when it comes to combining them. I am unsure if it's even possible. I have a Python script that can take input, perform calculations, and return a val ...

Customize Your Contact Form in Wordpress with a Stylish 2-Column Layout

Hey there, I've encountered a little problem with my website. My website is built on Wordpress and uses Contact Form 7, which has been heavily customized with CSS. While it looks great on desktop, the email field appears too small on mobile devices. I ...

Tips for creating a disabled appearance for a font awesome icon button:

I'm currently using a font awesome icon button and I'm looking to create a disabled state for the icon. Although I can achieve a disabled button appearance using the Bootstrap disabled class, the button remains clickable. Is there a way to make i ...

Tips for reaching the outer div utilizing the URL

I am working with an HTML script that looks like this: <html> <head></head> <body> <div id="div1"></div> <div id="div2"></div> </body> </html> Now, I need to be a ...

What could be causing the blur effect to not show up on the dropdown menu?

In designing the mobile version of my website, I encountered an issue with applying a blurred background to both the header and menu-content. Strangely, when one element is blurred, the other loses its blur effect. Below are snippets of CSS, HTML, and JS f ...

What is the art of spinning a div?

Looking to add an interactive circular div on my website that can be spun like a wheel of fortune using the mouse. I tried using jQuery drag without success in rotating the div. Is there a method to rotate div elements with the mouse? It would also be awe ...

Having trouble with the click button flip function? It seems to be working in one section but not in

Currently, I am facing an issue with a card section that contains two buttons and a description. The first button adds an image which is working perfectly fine, as well as the description section. On the other hand, the second button adds a video and when ...

CSS issue: The datetimepicking field is positioned behind the other input fields on my form

Currently struggling with formatting my table that is deployed via jquery in front of the input fields. I believe CSS could solve this issue, but I'm having trouble pinpointing the exact adjustments needed. (refer to image below) Utilizing eonasdan&a ...

Export MySQL table to HTML format

I am currently facing an issue regarding exporting MySQL data to HTML. The problem arises when one of the fields contains values formatted as <a href="http://google.com">Google</a>. Upon exporting the table in HTML format, the generated HTML ta ...

I seem to be having trouble getting my style to work properly with Material UI's makeStyles

I am experiencing an issue with Material-UI makeStyles not working properly. I am trying to apply my CSS style but it doesn't seem to be taking effect. I suspect that Bootstrap or MaterialTable might be causing this problem. While Bootstrap4 works fin ...

Having trouble aligning material-ui GridList in the center

I am currently working with a GridList in order to display Cards. The styling for these components is set up as shown below: card.js const useStyles = makeStyles({ card: { maxWidth: 240, margin: 10 }, media: { heigh ...

Utilize JQuery scripting to input Checkbox values

Using this JQUERY code to populate data into an HTML table. The first time it displays checkbox values, but the next time it does not show the values that were shown the first time. See the JQUERY code below: $(document).ready(function(){ $('#det ...

I've been attempting to access the Marketo API, but unfortunately, I haven't had any luck

I've been attempting to make a Marketo API call, but I keep encountering this issue: net::ERR_NAME_NOT_RESOLVED Here is the code snippet I'm using for the function: var marketoAPIcallURL = 'https://182-EMG-811.mktorest.com/rest/v1/ ...

What is the best way to rearrange <OL> list according to screen size using Bootstrap 4?

I have a unique layout in my HTML code where items are displayed vertically on small devices. However, when the device size is medium, the items shift to a horizontal view using Bootstrap's 'col-md-6' class. 1 2 3 4 5 6 7 8 9 I am looking ...

jQuery text alignment breaking bug

I am attempting to create an overlay for a couple of my divs to prevent users from progressing without completing previous tasks. The overlay is functioning correctly and the message is displaying, but I encounter issues when trying to center the message. ...