Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS3 :not selector

CSS3 :not selector

Complete CSS SelectorComplete CSS Selector Reference Manual

Example

Set the background color for every element that is not a

element:

:not

(

p

)

{

color:

#ff0000

;

}

Try it »


Definition and Usage

:not(selector) selector matches every element that is not the specified element/selector.

The CSS :not pseudo-class allows you to select all elements except the specified selector. This is a way to apply styles by excluding specific elements.

Basic Syntax

css

:not(selector) {

  /* Style rules */

}

** 1. Exclude elements of a specific type**



body :not(p) {

  color: blue;

}

This code sets the text color of all elements inside the body element except p elements to blue.

2. Exclude elements with a specific class

div :not(.highlight) {

  background-color: gray;

}

This code sets the background color of all elements inside div elements that do not have the highlight class to gray.

** 3. Exclude multiple selectors**



:not(div, span) {

  font-size: 14px;

}

This code sets the font size of all elements on the page that are neither div nor span to 14 pixels.

4. Exclude pseudo-classes

a:not(:hover) {

  color: black;

}

This code sets the text color of all links that are not in the hover state to black.

** 5. Exclude attribute selectors**

input:not([type="text"]) {

  border: none;

}

This code removes the border from all input elements that do not have type="text".

Note


Browser support

The numbers in the table indicate the first browser version that supports the selector.

Selector
:not() 4.0 9.0 3.5 3.2 9.6

Complete CSS SelectorComplete CSS Selector Reference Manual