Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting everything except one element [closed]

I'm trying to discover a way of selecting all elements except one specific element. In this case, the :not(selector) isn't working.

What's the best way to select all elements inside a container except for one of the specified elements?

.container not:(.test2) {
  color: red
}
<div class='container'>
  <p class='test'>test</p>
  <p class='test'>test</p>
  <p class='test'>test</p>
  <p class='test'>test</p>
  <p class='test2'>test 2</p>
</div>
like image 256
user8758206 Avatar asked Apr 27 '18 13:04

user8758206


People also ask

How do you select all elements except the last one?

It's quite easy to do this using the :not and :last-child pseudo-classes. The :not pseudo-class specifies elements that do not match a list of selectors and the :last-child selects the element if it is the last child among the other elements.

How do you exclude an element from a universal selector?

If you want to exclude root elements like html / body , you could select all elements within them. To exclude other elements, you could use the :not() pseudo-class to negate them. For instance, you could add an . excluded class to the elements.

How do I select all elements except one in CSS?

Approach: Use the :not(selector), also known as negation pseudo-class which takes a simple selector as an argument and allows you to style all the elements except the element specified by the selector. We can never use nested negation :not(:not(selector)) because pseudo-elements are not simple selectors.

How do you select all but last element in CSS?

When designing and developing web applications, sometimes we need to select all the child elements of an element except the last element. To select all the children of an element except the last child, use :not and :last-child pseudo classes.


3 Answers

Correct the syntax (add ":" in front of "not"):

.container :not(.test2) {
  color: red;
}
<div class='container'>
<p class='test'>test</p>
<p class='test'>test</p>
<p class='test'>test</p>
<p class='test'>test</p>
<p class='test2'>test 2</p>
</div>
like image 98
zolv Avatar answered Oct 02 '22 16:10

zolv


This is the correct syntax, you should match the siblings instead of the parent..

.container *:not(.test2) {
  color: red
}
<div class='container'>
<p class='test'>test</p>
<p class='test'>test</p>
<p class='test'>test</p>
<p class='test'>test</p>
<p class='test2'>test 2</p>
</div>
like image 22
Renzo Calla Avatar answered Oct 02 '22 15:10

Renzo Calla


Use

.container :not(.test2) {
        color: red;
 }

To select everything except .test2

All pseudo classes begin with a :

like image 44
Ahmed Masud Avatar answered Oct 02 '22 14:10

Ahmed Masud