Tagged: selectors

  • Pseudo-elements are not valid inside :is(), :where(), and &

    A pseudo-element is not a valid selector inside :is() or :where(). As per the Selectors Level 4 spec:

    Pseudo-elements cannot be represented by the matches-any pseudo-class; they are not valid within :is().

    The nesting selector & behaves the same way. It desugars to the parent rule’s selector list wrapped in :is().

    However, neither of them fails loudly. :is() and :where() take a forgiving selector list, so an invalid argument is dropped and the rest of the rule carries on. Similarly, nesting inside a pseudo-element rule parses fine. The nested style rule just never matches anything, unless the parent list also contains non-pseudo-element selectors.

    /* exmaple 1 */
    .card::before {
    	content: '';
    
    	.dark & {
    		color: red; /* `&` can't represent `.card::before`, so this matches nothing */
    	}
    }

    Either hoist the element out to the top level.

    /* exmaple 2 */
    .card::before {
    	content: '';
    }
    
    .dark .card::before {
    	color: red;
    }

    Or restructure so & refers to the element and the pseudo-element is nested in it.

    /* exmaple 3 */
    .card {
    	&::before {
    		content: '';
    	}
    
    	.dark &::before {
    		color: red;
    	}
    }

    A pseudo-element in the parent selector list doesn’t poison the whole list. & drops just that entry and still represents the rest.

    /* exmaple 4 */
    .card,
    .card::before {
    	color: black;
    
    	.dark & {
    		color: red; /* matches `.card`, but not `.card::before` */
    	}
    }

    The nested rule desugars to .dark :is(.card, .card::before), and the forgiving list throws out the pseudo-element, leaving .dark :is(.card). So the element turns red while its ::before stays black.

    From Chrome 130 / Safari 18.2 / Firefox 132 and onwards, bare declarations nested inside an at-rule are the exception. They do apply to the pseudo-element.

    /* exmaple 5 */
    .card::before {
    	content: '';
    	color: black;
    
    	@media (prefers-color-scheme: dark) {
    		color: white; /* applies to `.card::before` */
    	}
    }

    Wrap those same declarations in & { ... } and they stop applying.

    /* exmaple 6 */
    .card::before {
    	content: '';
    	color: black;
    
    	@media (prefers-color-scheme: dark) {
    		& {
    			color: white; /* matches nothing */
    		}
    	}
    }

    So why does example 6 not work?

    The reason is that declarations can’t sit loose inside a nested at-rule as far as the CSSOM is concerned. A CSSMediaRule exposes rules, and declarations only live on a style rule, so the parser has to wrap them in something. Examples 5 and 6 differ in what that something is.

    Example 5 wraps them in a nested declarations rule:

    It matches the exact same elements and pseudo-elements as its parent style rule, with the same specificity behavior.

    Whereas example 6 wraps them in & { ... } and & obeys selector rules: it desugars to :is(.card::before), which can’t represent the pseudo-element, so nothing matches.

    Further reading:

  • Inherited properties leak through the donut scope

    Today I learned about a potential pitfall with @scope.

    Via MDN [emphasis mine]:

    It is important to understand that, while @scope allows you to isolate the application of selectors to specific DOM subtrees, it does not completely isolate the applied styles to within those subtrees. This is most noticeable with inheritance — properties that are inherited by children (for example color or font-family) will still be inherited, beyond any set scope limit.

    In other words, @scope isolates your selectors, not your styles. Here’s what that looks like in practice.

    <article class="card">
    	<p>In scope</p>
    	<div class="content">
    		<p>Outside the scope limit</p>
    	</div>
    </article>
    @scope (.card) to (.content) {
    	:scope {
    		color: red;
    		font-family: Georgia, serif;
    	}
    
    	p {
    		padding: 1lh;
    		outline: 1px solid;
    	}
    }

    The second <p>, the one inside .content, won’t get the padding or outline because those are non-inherited properties in the scope limit. That part works exactly as you’d expect.

    But it will still turn red and render in Georgia because color and font-family are inherited properties set on .card (via :scope). And as we just learned from MDN, inheritance flows through the DOM regardless of where @scope sets the scope limit.

    As Miriam mentioned in the Winging It episode on CSS Scope & Mixins, it has to work this way because if @scope blocked inheritance at the scope limit, every element beyond the scope limit would receive the initial value for each inherited property.

    initial applies the value as defined in the CSS spec. And that would be far more destructive than letting inheritance flow through.

    If you’re coming from JavaScript, you might expect custom properties to work like scoped variables work in JavaScript. But in CSS, custom properties are inherited properties just like color or font-family.

    @scope (.card) to (.content) {
    	:scope {
    		--text-color: red;
    		font-family: Georgia, serif;
    	}
    
    	p {
    		color: var(--text-color);
    		padding: 1lh;
    		outline: 1px solid;
    	}
    }
    
    /* This rule exists to show that the inherited value is available beyond the scope limit */
    .content p {
    	color: var(--text-color);
    }

    With the same HTML, the second <p> again loses the padding and outline. But --text-color inherits down from .card into .content p. So the second <p> also turns red because its color property references var(--text-color) which resolves to red.

    For custom properties, if inheritance were blocked at the scope limit and no ancestor outside the scope had also defined it, any var(--text-color) beyond the scope limit would be undefined and that would just trigger the fallback in the var() function, or if there’s no fallback, the property using it would behave as unset.

    There’s more to what happens when a custom property is undefined, involving the guaranteed-invalid value and invalid at computed-value time (IACVT). I wrote a note on what happens when the CSS function var() references an undefined custom property.

  • Selector scoping in element.querySelector() and element.querySelectorAll()

    I was reading the MDN docs for element.querySelector() and learnt something totally unexpected about selector scope when this method is called on an element.

    Let’s say I have the following HTML:

    <div>
    	<p>This is a paragraph and it has a <span>span inside</span> it.</p>
    </div>
    
    <script>
    	const baseElement = document.querySelector('p');
    	// Note: We are querying relative to the <p> element, not the document
    	const matchedSpan = baseElement.querySelector('div span');
    	console.log(matchedSpan);
    </script>

    Once the above JavaScript is executed, what do you expect to happen?

    I expected it to log null to the console. My reasoning was simple: I am calling the method on baseElement (the <p> tag), and that paragraph clearly does not contain a <div> inside it.

    But here’s what actually gets logged to the console:

    <span>span inside</span>

    This surprisingly returns the <span> from inside the <p>.

    Why does this happen? MDN explains it like this:

    […] selectors is first applied to the whole document, not the baseElement, to generate an initial list of potential elements. The resulting elements are then examined to see if they are descendants of baseElement.

    Note: element.querySelectorAll() also applies selector scoping in the same manner.

    If we want to restrict the selector to the element on which it is called then we should use the :scope pseudo-class.

    const scopedQuery = baseElement.querySelector(':scope div span');
    console.log(scopedQuery); // Returns null, as expected

    As mentioned in the MDN docs for :scope, this works because:

    When used within a DOM API call — such as querySelector(), querySelectorAll(), matches(), or Element.closest() — :scope matches the element on which the method was called.

  • Note - Posted on

    Today I learned, :root (0-1-0) has a higher specificity than html (0-0-1).

    In hindsight, it’s obvious. :root is a CSS pseudo-class selector and, like most pseudo-class selectors, it has the same specificity as a class selector or an attribute selector.