CSS :not(selector) property


The :not(selector) pseudo-class in CSS is used to apply styles to elements that do not match a given selector. This pseudo-class allows for the exclusion of specific elements from certain styles, providing a way to apply styles to a broader range of elements while excluding others.

Syntax

selector:not(excluded-selector) { /* styles */ }
  • selector: The main selector that targets elements you want to style.
  • :not(excluded-selector): The pseudo-class that excludes elements matching the excluded-selector from receiving the styles applied to the selector.

How It Works

  1. Basic Usage:

    • The :not(selector) pseudo-class can be used to exclude specific elements from a broader set of styles.
    p:not(.exclude) { color: blue; }
    • In this example, all <p> elements that do not have the class exclude will be styled with blue text. <p> elements with the exclude class will not be affected by this rule.
  2. Combining Selectors:

    • You can combine :not() with other selectors to refine your styling.
    div:not(.highlight) { background-color: lightgray; }
    • This example applies a light gray background to all <div> elements except those with the class highlight.
  3. Using Multiple Selectors:

    • You can also use multiple selectors inside :not(), but only one :not() is allowed in a selector group.
    ul li:not(.active):not(.completed) { color: gray; }
    • This targets <li> elements within <ul> that do not have the class active and do not have the class completed, applying gray color to them.

Examples

HTML

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Not Selector Example</title> <style> p:not(.highlight) { color: gray; } div:not(.special) { border: 1px solid black; } ul li:not(:last-child) { margin-bottom: 10px; } </style> </head> <body> <p>This paragraph will be gray.</p> <p class="highlight">This paragraph will not be gray.</p> <div>This div will have a border.</div> <div class="special">This div will not have a border.</div> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> </body> </html>

Explanation:

  • p:not(.highlight):

    • Applies gray text color to all <p> elements that do not have the class highlight.
  • div:not(.special):

    • Applies a border to all <div> elements that do not have the class special.
  • ul li:not(:last-child):

    • Applies a bottom margin to all <li> elements within <ul> except for the last child.

Important Points

  1. Single Level Only:

    • :not() only works on a single level of selectors and cannot be nested within other pseudo-classes like :nth-child.
  2. Performance Considerations:

    • Using :not() with complex selectors can impact performance, particularly with large documents. Keeping :not() selectors simple can help maintain performance.
  3. Combining with Other Pseudo-Classes:

    • :not() can be used with other pseudo-classes to create more specific and refined styling rules.
    div:not(:first-child) { padding: 10px; }
    • This example applies padding to all <div> elements that are not the first child of their parent.