Color scheme

Modern Selectors

CSS selectors have come a long way from the days of chaining element names and classes into long, fragile strings. Modern pseudo-class functions like :is(), :where(), :has(), and :not() let you express complex selection logic in compact, readable rules while keeping specificity under control. Combined with the focus-state pseudo-classes :focus-visible and :focus-within, and the @supports at-rule for feature detection, these tools give you a precise, future-proof toolkit for writing CSS that is both powerful and maintainable.

Simplifying Selectors with :is() and :where()

Both :is() and :where() accept a comma-separated list of selectors and match any element that matches at least one of them. They exist to solve the same problem: compound selectors that repeat shared context over and over. The only difference between them is how they affect specificity.

Before and After

Consider a rule that styles links inside several heading levels. Without :is() you have to write every combination out:


        /* Without :is() — verbose and repetitive */
        article h1 a,
        article h2 a,
        article h3 a,
        article h4 a {
            color: var(--link-color);
            text-decoration: none;
        }
    

With :is() the same rule collapses to a single line:


        /* With :is() — same result, much shorter */
        article :is(h1, h2, h3, h4) a {
            color: var(--link-color);
            text-decoration: none;
        }
    

Both versions match exactly the same elements. The :is() version is easier to read, easier to maintain, and less prone to typos when you need to add or remove heading levels.

The Specificity Difference

The key distinction between :is() and :where() is specificity. :is() takes the specificity of its most specific argument. If you write :is(.card, #sidebar), the entire :is() group contributes the specificity of an ID selector because #sidebar is the most specific item in the list.

:where() always contributes zero specificity, no matter what you put inside it. This makes it ideal for base styles and resets that you want to be easy to override later.


        /* :is() — takes the specificity of its highest argument */
        :is(.card, .panel) p {
            /* Specificity: 0-2-1 (two classes + one element) */
            color: #333;
        }

        /* :where() — always zero specificity */
        :where(.card, .panel) p {
            /* Specificity: 0-0-1 (only the p element counts) */
            color: #333;
        }
    
When to Use Which

Use :is() when you want the grouped selector to carry its natural weight in the cascade. Use :where() when you are writing defaults, resets, or base layer styles that should be trivially overridable by any class.

Forgiving Selector Lists

Both :is() and :where() use a forgiving selector list. If one of the selectors inside the list is invalid, the browser ignores just that selector instead of invalidating the entire rule. In a normal comma-separated selector list, a single invalid selector causes the whole block to be dropped. This forgiving behavior makes :is() and :where() safer to use when experimenting with newer selectors that might not be supported everywhere.

Selecting by Relationship with :has()

For years, CSS could only select elements based on their ancestors, siblings, or their own state. There was no way to select a parent based on what children it contained. The :has() pseudo-class changes that. It selects an element if any of the relative selectors passed as its argument match at least one element when anchored against it.

In simpler terms, :has() lets you style a parent based on its children, or an element based on what follows it. This was previously only possible with JavaScript.

Parent Selection

The most common use case is styling a container differently depending on what it contains. For example, you might want a card component to look different when it includes an image:


        /* Cards with images get a different layout */
        .card:has(img) {
            display: grid;
            grid-template-rows: auto 1fr;
        }

        /* Cards without images stay as a simple stack */
        .card:not(:has(img)) {
            padding: 2rem;
        }
    

Sibling and Adjacency Patterns

You can combine :has() with combinators to match based on what follows an element. A heading that is immediately followed by a subtitle might need less bottom margin:


        /* Reduce margin when a subtitle follows the heading */
        h2:has(+ .subtitle) {
            margin-bottom: 0.25rem;
        }
    

Live Demo: :has() with a Checkbox

The demo below shows :has() in action without any JavaScript. When you check the box, the parent container detects the checked state through :has(input:checked) and changes its appearance. Toggle the checkbox to see the effect:

The parent container has no special styling.

The parent detected :checked and changed its background and border.

Here is the CSS that powers the demo. No JavaScript is involved at all:


        .container {
            padding: 1.5rem;
            border: 2px solid #888;
            transition: background-color 0.3s, border-color 0.3s;
        }

        /* Parent reacts to child state */
        .container:has(input:checked) {
            background-color: #dcfce7;
            border-color: #15803d;
        }
    

Like :is() and :not(), the specificity of :has() is determined by its most specific argument. Keep that in mind when using it in combination with other selectors.

Filtering with :not()

The :not() pseudo-class has been around longer than the others, but the modern version (Selectors Level 4) accepts a full selector list, just like :is(). This makes it far more useful than the original single-argument version.


        /* Style all form inputs except checkboxes and radios */
        input:not([type="checkbox"], [type="radio"]) {
            display: block;
            width: 100%;
            padding: 0.5rem;
        }
    

A common practical pattern is using :not() to avoid styling the last item in a list:


        /* Add a bottom border to every nav link except the last one */
        .nav-link:not(:last-child) {
            border-bottom: 1px solid var(--color-border);
        }
    

Specificity Gotcha

Just like :is() and :has(), the specificity of :not() is equal to the specificity of its most specific argument. Writing :not(#something) gives the entire :not() the weight of an ID selector, even though you are excluding that ID rather than selecting it. Be intentional about what you put inside :not() to avoid unintentionally inflating your specificity.

Accessible Focus States

Keyboard accessibility depends on visible focus indicators. When a user tabs through interactive elements, they need a clear visual signal showing which element is active. CSS provides two modern pseudo-classes that help you build focus styles that are both accessible and aesthetically polished.

:focus-visible

The :focus-visible pseudo-class matches an element only when the browser determines that focus should be visually indicated. In practice, this means it activates on keyboard navigation (tab, shift+tab) but not on mouse clicks. This lets you show a prominent focus ring for keyboard users without adding visual clutter for mouse users.


        /* Remove the default outline for mouse users */
        button:focus {
            outline: none;
        }

        /* Show a strong ring only for keyboard users */
        button:focus-visible {
            outline: 3px solid var(--color-focus);
            outline-offset: 2px;
        }
    

:focus-within

The :focus-within pseudo-class matches a container when any element inside it has focus. This is useful for form groups or navigation menus where you want the entire wrapper to respond when a child input or link is focused.


        /* Highlight the entire form group when an input inside it is focused */
        .form-group:focus-within {
            border-color: var(--color-focus);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
        }
    

Live Demo: Focus States

Use your keyboard to tab through the buttons below. The first row uses :focus (always shows the ring). The second row uses :focus-visible (ring appears on keyboard navigation but not on click). Try clicking a button and then tabbing to the next one to see the difference:

:focus (always shows ring)
:focus-visible (ring on keyboard only)

Here is the CSS behind the demo:


        /* Always shows the ring, even on click */
        .always-ring:focus {
            outline: 3px solid #2563eb;
            outline-offset: 2px;
        }

        /* Only shows the ring on keyboard navigation */
        .keyboard-ring:focus {
            outline: none;
        }
        .keyboard-ring:focus-visible {
            outline: 3px solid #2563eb;
            outline-offset: 2px;
        }
    
Never Remove Focus Without Replacing It

Setting outline: none on :focus without providing a :focus-visible style makes your site unusable for keyboard users. Always pair the removal with a visible alternative. Accessibility audits and WCAG compliance require a visible focus indicator for all interactive elements.

Progressive Enhancement with @supports

The @supports at-rule lets you test whether a browser supports a particular CSS feature before using it. This is called a feature query, and it is the CSS equivalent of checking for support before running a function in JavaScript. When you are writing cutting edge CSS or future-proofing a site, @supports gives you a safe way to provide enhanced styles for capable browsers while keeping a solid baseline for everything else.

Basic Syntax

A feature query wraps a set of declarations inside a condition. If the browser supports the tested declaration, the rules inside apply. If not, they are ignored and the baseline styles remain in effect.


        /* Baseline layout */
        .grid {
            display: flex;
            flex-wrap: wrap;
            gap: 1rem;
        }

        /* Enhanced layout for browsers that support subgrid */
        @supports (grid-template-columns: subgrid) {
            .grid {
                display: grid;
                grid-template-columns: repeat(3, 1fr);
            }
            .grid > .card {
                grid-template-rows: subgrid;
            }
        }
    

The baseline uses flexbox, which works everywhere. Browsers that support subgrid get the enhanced grid layout. Browsers that do not simply skip the @supports block entirely.

Testing for Selectors

You can also use @supports with the selector() function to test whether the browser understands a particular selector:


        /* Only apply if the browser supports :has() */
        @supports selector(:has(*)) {
            .card:has(img) {
                grid-template-rows: auto 1fr;
            }
        }
    

This is especially useful for newer pseudo-classes like :has() where you want to avoid the entire rule block being dropped in browsers that do not recognize the selector.

Combining Conditions

Feature queries support and, or, and not operators, so you can test multiple features at once or provide styles specifically for browsers that lack support:


        /* Both features must be supported */
        @supports (display: grid) and (container-type: inline-size) {
            /* grid with container queries */
        }

        /* Fallback for browsers without container query support */
        @supports not (container-type: inline-size) {
            .sidebar {
                width: 300px;
            }
        }
    

The Progressive Enhancement Mindset

The approach is the same one you practiced with mobile-first media queries in CSS Fundamentals: start with a baseline that works everywhere, then layer on enhancements for more capable environments. Write your core styles first without any @supports blocks, then wrap the cutting edge additions in feature queries. This way every user gets a functional experience, and users with modern browsers get the enhanced version.

Putting It Together: Reducing Selector Weight

Now that you have seen each of these tools individually, here is how they work together to keep your selectors simple and your specificity flat. Consider a navigation component where you need to style links, handle focus states, and guard a newer feature:


        /* Low specificity base using :where() */
        :where(nav) a {
            color: var(--nav-link-color);
            text-decoration: none;
            padding: 0.5rem 1rem;
        }

        /* Keyboard focus ring */
        nav a:focus-visible {
            outline: 3px solid var(--color-focus);
            outline-offset: 2px;
        }

        /* Highlight the nav when any link is focused */
        nav:focus-within {
            box-shadow: 0 0 0 2px var(--color-focus-soft);
        }

        /* Active state for all link types at once */
        nav :is(a, button):hover {
            color: var(--nav-link-hover);
        }

        /* Style links that are not the current page */
        nav a:not([aria-current="page"]) {
            opacity: 0.8;
        }

        /* Enhanced: highlight the nav item whose dropdown is open */
        @supports selector(:has(*)) {
            nav li:has(.dropdown[open]) {
                background-color: var(--nav-active-bg);
            }
        }
    

Every selector in this block is short and intentional. The base uses :where() so it is trivially overridable. :is() groups hover targets. :not() filters out the current page link. :focus-visible and :focus-within handle keyboard accessibility. And @supports guards the :has() rule so older browsers simply skip it. No IDs, no deeply nested selectors, no !important.

Resources

The following references were used throughout this reading. Each is worth bookmarking for when you need to look up syntax or browser support details.