Is it possible to exclude elements that are not something or something else using :not()?

Let the example do the talking

.cyan {
  background-color: cyan;
}
.red {
  background-color: red;
}
.green {
  background-color: green;
}
.blue {
  background-color: blue;
}
.yellow {
  background-color: yellow;
}

li:not(.yellow), li:not(.green) {
  color: white;
}
<ul>
  <li class="cyan">red</li>
  <li class="red">red</li>
  <li class="green">green</li>
  <li class="blue">blue</li>
  <li class="yellow">yellow</li>
</ul>

I want to keep cyan and yellow as they are. I am aware of other methods, but that's not what I'm looking for.

I wish to apply :not(.yellow or .green)

Answer №1

To create a series of negation selectors using :not, you can chain them together like this:

.purple {
  background-color: purple;
}
.orange {
  background-color: orange;
}
.pink {
  background-color: pink;
}
.brown {
  background-color: brown;
}
.grey {
  background-color: grey;
}

li:not(.grey):not(.pink) {
  color: black;
}
<ul>
  <li class="purple">purple</li>
  <li class="orange">orange</li>
  <li class="pink">pink</li>
  <li class="brown">brown</li>
  <li class="grey">grey</li>
</ul>

Answer №2

Here's the solution for you

li:not(.yellow):not(.green) {
  color: white;
}

Answer №3

.teal {
  background-color: teal;
}
.pink {
  background-color: pink;
}
.orange {
  background-color: orange;
}
.purple {
  background-color: purple;
}
.gray {
  background-color: gray;
}

li:not(.gray):not(.purple) {
color: black;
}
<ul>
  <li class="teal">pink</li>
  <li class="pink">pink</li>
  <li class="orange">orange</li>
  <li class="purple">purple</li>
  <li class="gray">gray</li>
</ul>

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

Using the columns property in CSS3 along with background-color

My ul-element has multiple lis with a background color assigned to it. I am using the CSS3 columns property and everything is working well, but the background color of my ul is only visible in the first column. What could be causing this issue? Thank you ...

Adjusting the width of a select box to match the size of its child table using CSS

Within my Vue application, I've implemented a select box that contains a table component. When the select box is clicked, the table becomes visible in a container. However, I'm facing an issue where I can't dynamically adjust the width of th ...

Disappearance of icon upon hovering in CSS

I am currently working with Angular JS (1.x) and I have a requirement to display tooltip text when hovering over an info icon. The tooltip text appears properly, but the issue is that the icon disappears when hovered over. It reappears once the hover is re ...

Disappear Image Overlay when Mouse Hovers

Currently, I am in the process of working on a new webpage. You can view the progress at the following link: . When hovering over the Referrals Button, a Background Overlay will appear. To enhance the visual effect, I am looking to implement a gradual appe ...

Issue with the react-native-autocomplete-input where the list does not close after selecting an option

After selecting an item from the list in my react-native-autocomplete-input, the list does not close. let Location = (props) => ( <View style={styles.formItem}> <Autocomplete data={props.autocompleteResults.predictions} de ...

menu featuring a creative favicon and an accompanying label

I am aiming to replicate this menu design: It consists of a flavicon with a label below it. Menu I attempted to create it in two different ways, but both attempts were unsuccessful. Using Bootstrap groups. <link rel="stylesheet" href="https://u ...

Using Foreach with Bootstrap to display a grid of 6 column items with images

When using a foreach loop to display clients with images, the desired layout is for them to be listed in 6 columns. For instance, if there are 18 clients, they should be displayed in a grid of 6 columns and 3 rows. Here is the Razor code and HTML: <di ...

Tips for altering text size within Angular Material form fields with floating placeholder

Looking to customize the font size of the placeholder text in an Angular Material form field? You can set two different font sizes: one for when the placeholder is normal and one for when it floats. Here's how you can achieve this: <mat-form-fiel ...

Just a single page on my website extends beyond the footer's reach

Hey there, I'm pretty new to this whole HTML/CSS thing and need some help. I have a website called cme.horse and everything seems to be running smoothly except for the Home page (main/index page). The issue is that it scrolls past the footer, while al ...

What are the steps to create a customized app bar with React and Material-UI similar to this design?

Can anyone help me create an app bar that resembles this design: Click here to view I have managed to implement the search box in the top half of the app bar, but I am struggling with adding the bottom half. Here is the code I have written so far: ...

Tips for implementing a sliding list feature with next and previous buttons:

Currently, I am utilizing a list attribute to display thumbnail images of a YouTube-generated playlist (gdata feed). However, my goal is to enclose the list within a div container and implement next and previous buttons to slide through the playlist images ...

After examining the width properties in the CSS code, it was discovered that one particular icon is larger in

Is there a way to adjust the width of the first icon in my container using CSS? The first icon appears larger than the others, and I'm having trouble making it the right size. #features { margin-top: 35px; } .grid { display: flex; } #feat ...

Is there a left and right margin when incorporating Bootstrap grid?

Currently, I am working on developing an Angular application and utilizing Bootstrap columns to easily format and space my page. However, I am facing an issue where there seems to be some unwanted margin around the element even though I have applied col-lg ...

Get rid of the rollover effect on the <a> tag when hovering over an image

I have a snippet of code that is fully functional. <div class="user-pic round-pic"> <a href="<?php echo $userLoggedIn; ?>"><img src="<?php echo $user['profile_pic']; ?>"></a> </div> The issue lies in i ...

Steps to modify the background-color of an iFrame using CSS

I am attempting to extract HTML code from an iframe and apply a background-color to the class: class="body" The structure of my HTML source is as follows: <iframe id="sc_ext_XT29EK" class="sc_ext" width="100%" height="1300px" frameborder="0" src="some ...

How can I detect a DOM element mutation based on a CSS selector, and if this is possible, how can it be accomplished?

Imagine there's a website with a specific HTML element. It seems that this element has the same class during the DOMContentLoaded event as it does during the load event. However, after the load event, this class (and possibly the ID and other HTML att ...

What is the best way to display two items side by side from a list of items in HTML?

I have a list of items that are displayed in `webView` with 4 elements per row. However, for `mobileView`, I would like to show only 2 elements in each row. Below is an example from my HTML file: <div class="row my-3 cust-heading" style="margin-left: 3 ...

Setting maximum and minimum zoom limits for an element ID using JavaScript or jQuery

My application features a DIV element with the unique identifier of mainDiv. The issue I am facing is related to zooming functionality, as it currently lacks any set limits - both for scaling up and scaling down. I have been searching on Google for a sol ...

A slender gap of white space delicately separates the transformed parent from its offspring

When trying to align a parent div with a child div that has the same background-color as the parent's border-color, I am encountering an issue. Despite my efforts, there seems to be a thin line of whitespace separating the two divs. It is worth notin ...

Utilizing jQuery to generate dynamic nested divs within the ul li tags

I am looking to dynamically generate nested divs within a ul li. The goal is to create the following structure programmatically: <ul> <li> <div class="videoThumbnail"> <img src="./img6.jpg" /> &l ...