Skip to content

[css-nesting] Make @nest default to descendant when no & is present #5738

@LeaVerou

Description

@LeaVerou

https://drafts.csswg.org/css-nesting/#at-nest

The @nest rule functions identically to a style rule: it starts with a selector, and contains declarations that apply to the elements the selector matches. The only difference is that the selector used in a @nest rule must be nest-containing, which means it contains a nesting selector in it somewhere. A list of selectors is nest-containing if all of its individual complex selectors are nest-containing.

Since there is no ambiguity with @nest, neither syntactic, nor ambiguity of intent, why still force &? We can just default to the descendant selector, just like preprocessors.

This, is a little awkward:

.foo {
	@nest & .bar {
		color: white;
	}
}

compared to:

.foo {
	@nest .bar {
		color: white;
	}
}

Yes, in that case you don't actually need @nest, but some people may prefer it for the explicitness, or it could be generated. In any case, it's a sensible default, that is trivial to implement, and has already been tested for both utility and usability in preprocessors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Commenter SatisfiedCommenter has indicated satisfaction with the resolution / edits.css-nesting-1Current Work

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions