Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Mastering the :not() CSS Pseudo-Class
Matt Miller
Matt Miller

Posted on • Edited on

     

Mastering the :not() CSS Pseudo-Class

Introduction:
In CSS, the :not() pseudo-class serves as a powerful tool for selecting elements that do not match a given set of selectors. By negating specific selectors, :not() offers flexibility in styling and targeting elements in a document. However, its usage comes with nuances and unexpected outcomes that developers need to understand to leverage it effectively.

Syntax:
The syntax of the :not() pseudo-class is straightforward:

:not(<complex-selector-list>){/* CSS rules */}
Enter fullscreen modeExit fullscreen mode

Here,<complex-selector-list> represents a comma-separated list of one or more selectors that the element should not match.

Key Points:

  1. Useless Selectors: :not() can lead to writing selectors that are ineffective. For instance, :not(*) matches any element that is not an element itself, rendering the accompanying rule useless.
  2. Specificity Increase: It's essential to note that :not() can increase the specificity of a rule. This means that selectors like #foo:not(#bar) will have higher specificity due to the presence of multiple ID selectors.
  3. Replacement of Specificity: The specificity of :not() is determined by the most specific selector within its argument list, similar to :is(). It essentially adopts the specificity of the selectors it negates.
  4. Surprising Matching: :not() selects everything that is "not an X." This can lead to unexpected results, especially when used with descendant combinators, as there may be multiple paths to select a target element.
  5. Negating Multiple Selectors: Multiple selectors can be negated simultaneously using :not(). For example, :not(.foo, .bar) is equivalent to :not(.foo):not(.bar).
  6. Handling Invalid Selectors: If any selector within :not() is invalid or unsupported, the entire rule will be invalidated. To overcome this, the :is() pseudo-class can be used, as it accepts a forgiving selector list.

Advance instance

Conclusion:
The :not() pseudo-class is a valuable asset in the CSS arsenal, offering a means to target elements that do not match specific selectors. However, its usage requires careful consideration of its behavior and potential quirks to avoid unintended consequences. By understanding its syntax, intricacies, and best practices, developers can wield the :not() pseudo-class effectively to enhance the styling and structure of their web projects.


Enjoying the content? If you'd like to support my work and keep the ideas flowing, consider buying me a coffee! Your support means the world to me!

Buy Me A Coffee

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
annetawamono profile image
Anneta Wamono
Hi there 👋, I'm a web developer at Studio Locale and an aspiring technical writer. I'm also a 2D animator and story artist for my own little films.
  • Location
    Kitchener, Canada
  • Education
    University of Pretoria, Conestoga College
  • Pronouns
    She / Her
  • Work
    Web developer at Studio Locale
  • Joined

Great article! I'm always using :not() but you pointed out quite a few things I didn't know before, especially the specificity part.

CollapseExpand
 
matemiller profile image
Matt Miller
specialize in creating UI designs and dynamic UX interactions, enjoy communication and negotiation, and have also completed internships in multimedia and web design where networking was a key value.
  • Email
  • Location
    4713, Gallspach, Austria
  • Education
    Web developer (BS.c.) and trained multimedia designer
  • Work
    Full-stack Web Developer
  • Joined

You are very welcome! Will be more, CSS ninja stuff 🥷

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

specialize in creating UI designs and dynamic UX interactions, enjoy communication and negotiation, and have also completed internships in multimedia and web design where networking was a key value.
  • Location
    4713, Gallspach, Austria
  • Education
    Web developer (BS.c.) and trained multimedia designer
  • Work
    Full-stack Web Developer
  • Joined

More fromMatt Miller

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp