Can you tell me the opposite of a series of interconnected :nots in CSS selectors?

If I have several classes such as one, two, three, and four, how can I select DIVs that have class one but none of the classes two, three, or four using the following selector?

div.one:not(.two):not(.three):not(.four)

Is it possible to select the opposite (DIVs that have at least one of two, three, or four, but not one) in a single selector, or does it require multiple selectors like the example below?

div.two:not(.one), div.three:not(.one), div.four:not(.one)

Although the above example works, it involves a list of three selectors.

Answer №1

Is it possible to efficiently select the inverse of a certain element using only one selector?

One way to achieve this is by using the :any() pseudo-class

Currently, only a few browsers support the :any() pseudo-class, which will eventually be replaced by :matches

The syntax for using this feature is as follows:

:-moz-any( <selector># ) { style properties }
:-webkit-any( <selector># ) { style properties }

Therefore, the original selector can be refactored as:

div:-webkit-any(.two, .three, .four):not(.one) 

Check out a demo showcasing the use of :any() with the provided syntax.

div:-webkit-any(.two, .three, .four):not(.one) { 
  width: 100px;
  background-color: red;
}

div{
  width:100px;
  border: 1px solid black;
}
<div class="one">1</div>
<div class="two one">2</div>
<div class="three">3</div>
<div class="four one">4</div>

https://i.sstatic.net/epFo2.png

Potential Future Options.

Another upcoming option is the :matches() selector

The :matches pseudo-class is described as a functional pseudo-class by the official CSS Selectors Level 4 specifications. It doesn't serve any purpose in itself except making some complex selectors lighter by allowing them to be grouped. In a way, we can think as :matches as syntactic sugar.

This will allow for the following functionality:

div:matches(.two,.three,.four) :not(.one) { /*your styles*/}

Alternative Approaches

An alternative approach is to assign a common class, for example number, to all relevant elements

Subsequently, you can use the following selector:

div.number:not(.one)

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

ngClass causing styling issue when applying styles

When I use class names like error and info, the CSS works fine. For example, in this Angular app (latest version) here: https://stackblitz.com/edit/github-i4uqtt-zrz3jb. However, when I try to rename the CSS classes and add more styles, like in the examp ...

Exploring Vaadin 14 Slider Components

I'm looking for help on how to incorporate a slider into my Vaadin 14 project. I discovered that the slider component was removed in Vaadin 10, so I turned to this alternative: Once I added the Maven dependency and repository to my pom.xml, I success ...

Is there a CSS3 Selector With Similar Functionality to jQuery's .click()?

For a few years now, I have been utilizing a pure CSS navigation system. However, with the recent increase in mobile site projects at my workplace, I am encountering issues with drop-down menus not functioning properly on mobile devices. Despite this chall ...

What is the process for modifying two IDs simultaneously?

I'm struggling to include more than one ID in my CSS. I want the box border to have four different hover effects and three gradient buttons (e.g., play, pictures, etc.). Here is my HTML: <h2><a id="hover-1" href="">Hov ...

Rearranging the layout of elements: CSS switches up the text and button,

Having an issue. I created a background image with gradient colors for my web project and clipped it. Within this element, there is text and a button that I want to be centered inside the element - with the text above and the button below. Initially, ever ...

The attempt to showcase items in a div element using inline display did not produce

I am having trouble with creating a webpage. I have designed a website but I am struggling to get it right. I have included my HTML and CSS code below. What I am trying to achieve is a header at the top, followed by several sections containing a photo on ...

Utilizing mat dialog in conjunction with the bootstrap sticky top class to enhance functionality

I am encountering an issue where, upon clicking to delete an entry, a mat dialog should appear with everything else in the background greyed out. However, the problem I am facing is that when I attempt to delete an entry, the dialog appears but the sticky ...

Change all lowercase characters to uppercase in the font file

: I recently created a .ttf font file, but unfortunately only included capital characters. Is there a tool or simple method I could use to automatically create lowercase versions of each letter? For instance, when typing "hello" the font should display ...

Menu Drop on top of Flash player

A challenge I am currently facing is with my live event site and the list of events displayed in a mouseover menu. The issue arises when the flash streaming player remains in front of the mouseover menu, causing interference across all versions of Interne ...

What techniques can be used to resize an image to perfectly fit a square on a webpage?

A challenge on the web page is to display images in a square format of 90 * 90 pixels. However, the sizes of these images are not consistent and may vary from 80*100 to 100*80 or even 90 * 110. The requested solution is to stretch the image as follows: ...

Is there a way to implement hover behavior for a Material-UI Button within a ButtonGroup component?

When using MUI v5, I am encountering an issue where the first button in the code provided is only half working. The button is initially colored red (both the border and text), however, upon hovering over it, the color of the border changes to blue. This is ...

Basic HTML user interface elements

I'm struggling with some basic HTML/CSS and I'm looking for guidance on what I might be doing incorrectly or if there is a more efficient approach? I'm attempting to create a simple user interface, and while I have put this together, it doe ...

Cursor customized image vanishes upon clicking anywhere on the page on a MAC in various web browsers

I am currently trying to set a custom image as the cursor using jQuery in this manner $(document).ready(function(){ $("body").css('cursor','url(http://affordable-glass.com/test/penguinSm.png),auto'); }); While this method works, ...

Maintaining aspect ratio in CSS grid layouts

I am in the process of designing a CSS grid layout where I want to maintain the same height for all columns. Additionally, I would like the first item to have a 16:9 aspect ratio. However, I am encountering an issue where the images are not fitting perfect ...

Creating custom styles using Material-UI's makeStyles function with both before and after

Currently, I'm tasked with a project that involves utilizing the CSS code below. .hexagon, .hexagon::before, .hexagon::after { width: 67px; height: 116px; border-radius: 18%/5%; } I am wondering if there is a method to apply the specified style ...

Using clip-path in SVG works perfectly on images, but unfortunately it does not work on div

I came across an interesting example on MDN about using a clip-path SVG on an image. However, it seems that the same clip-path does not work properly when applied to a div. Can anyone shed some light on: The reason why this code is not achieving the inte ...

Obtaining the accurate offsetTop and offsetLeft values for an element following a CSS3 rotation

Is there a method to accurately determine the offsetTop and offsetLeft values of an element post-transform rotation? Are there any lesser-known element properties that could be helpful in this scenario? Attached is an image that can provide further clari ...

How to align the last item in the bottom row using Flexbox styling

Is it possible to center the last element when resizing the window to a smaller resolution, even though the parent's justify-content parameter is set to space-between? I understand that using center or space-around would achieve the desired result, bu ...

Manipulate CSS Properties with Javascript Based on Dropdown Selection

I am currently working on implementing a feature that involves changing the CSS property visibility: of an <input> element using a JavaScript function triggered by user selection in a <select> dropdown. Here's what I have so far in my cod ...

Scale transformation - I am aiming for it to exceed the limits, yet it remains contained within

Currently, I am working on enhancing my carousel by implementing a zoom effect when hovering over the images. However, I have encountered an issue where the image gets hidden within the div container and doesn't overflow as expected. I tried adjusting ...