Introducing a new way to review your PRs.

Read the announcement

less / less.js

Less. The dynamic stylesheet language.

View on GitHub

17 documents · Updated Sep 7, 2026

docs / selectors-extends-and-at-rules

Selectors, extends, and at-rules

Less turns nested selector and conditional-rule syntax into ordinary CSS by joining selector paths, resolving parent references, applying selector extensions, and flattening compatible at-rule scopes. These rules preserve the relationship between a component’s local styles and the CSS context in which those styles must apply.

Selector joining

Nested rules inherit the selector paths of their parent rules. A child selector without & is appended with a descendant relationship, while a child selector with & replaces that parent position. Each comma-separated parent and child choice is combined, so one nested rule can produce several CSS selectors. The join process also keeps separate path segments for nested relationships such as .a .b, which lets later selector features operate on the full path. See packages/…/ruleset.js• L673-841 and packages/…/ruleset.js• L1034-1065.

The parent selector & means the selector at the current nesting level. It can stand alone, join a class or pseudo-class, or appear before or after another selector. This makes forms such as &:hover, div &, and .item& produce different relationships instead of always adding a descendant. When no parent exists, the join keeps the selector usable in a mixin or other reusable rule without inventing a parent. See packages/…/ruleset.js• L849-1021.

flowchart TD
    P[Parent selector paths] --> J[Join child selector]
    C[Child selector] --> J
    A{Contains &?} -->|No| D[Append as descendant]
    A -->|Yes| R[Replace & with parent path]
    J --> A
    D --> O[Expanded CSS selector paths]
    R --> O

Parent references also work inside selector functions such as :not() and :is(). Less resolves the nested selector held by the function and then inserts the resulting parent-aware selector without treating the function as a separate CSS rule. A comma-separated selector list is expanded within that function while the function wrapper remains part of the selector. See packages/…/ruleset.js• L886-960 and packages/…/selectors.less• L205-251.

Extend processing

An :extend() declares that one selector should share the rules of another selector. Less records the extension while it builds selector paths, then adds matching selector paths to the rules that target the extended selector. The extending rule does not copy declarations into a new rule; the output rule keeps its declarations and gains the matching selector alternatives. See packages/…/extend.js• L6-55 and packages/…/extend-visitor.js• L260-296.

Extension matching respects selector elements and their combinators. The default form targets the matching selector in its expected position. The all option permits matches inside a larger selector, before or after other elements. This can add an extending selector to several related rules, including descendant, sibling, and child forms. See packages/…/extend.js• L27-43 and packages/…/extend-visitor.js• L298-373.

Extensions can chain. If one selector extends a second selector and that second selector extends a third, Less resolves the chain so the first selector can reach rules for the third. Circular chains are stopped and reported rather than expanded without limit. An extension with no match produces a warning and does not add output. See packages/…/extend-visitor.js• L146-245 and packages/…/extend-visitor.js• L123-143.

The extension scope follows nested at-rule scopes. An extension declared outside a media or container rule can affect matching selectors inside that scope, while an extension declared inside a scope is carried through that scope’s matching rules. The resulting selector stays under the same CSS condition as the rule it modifies. See packages/…/extend-visitor.js• L482-502 and packages/…/extend-media.less• L1-24.

Media and container conditions

@media and @container are nestable conditions around rules. Less evaluates their feature expressions, evaluates their child rules in the condition’s selector context, and emits the condition around the resulting CSS. A nested ordinary selector therefore keeps its parent selector when it moves inside the at-rule. See packages/…/media.js• L12-84 and packages/…/container.js• L19-98.

Nested conditions of the same kind are combined. Media query alternatives are treated as separate choices, and nested choices are permuted with the outer choices. Less joins each compatible choice with and, then places a media type before feature conditions when needed for valid CSS. Different at-rule kinds are not merged as media choices. See packages/…/nested-at-rule.js• L122-203.

The same nesting model applies to container queries. A container name or container feature expression remains part of the emitted @container condition. Nested rules gain the selectors from their parent rule, while nested containers remain nested when the CSS structure requires both a media and a container condition. See packages/…/container.js• L51-98 and packages/…/container.less• L1-48.

flowchart TD
    E[Outer condition] --> N[Nested condition]
    N --> K{Same at-rule kind?}
    K -->|Yes| P[Create condition combinations]
    K -->|No| S[Keep compatible at-rule nesting]
    P --> Q[Emit combined conditional rules]
    S --> Q

At-rule composition

Less distinguishes at-rules that contain declarations from at-rules that contain nested rules. A declaration block can emit directly inside its at-rule. A ruleset-like at-rule receives selector paths from the surrounding rules so its declarations stay attached to the correct CSS selector. See packages/…/atrule.js• L24-109 and packages/…/atrule.js• L150-177.

At-rule evaluation temporarily isolates media bubbling while it evaluates an at-rule’s own value and children. This prevents a media block nested inside another at-rule from escaping and being emitted at the wrong level. When the at-rule is complete, Less restores the surrounding condition state and emits the resulting block. See packages/…/atrule.js• L183-217.

Selector composition determines whether a nested at-rule’s child rules belong to the parent selector scope or form a rooted conditional block. Mixed parent-selector forms are kept from being flattened into an invalid single scope. The join visitor then marks the first child ruleset or declaration as rooted when the output needs a new at-rule boundary. See packages/…/atrule.js• L225-272 and packages/…/join-selector-visitor.js• L25-61.

When a nested condition is evaluated inside a selector, Less can wrap the condition in a ruleset carrying those selectors. This is what lets a local rule such as a selector with a nested @media or @container produce conditional CSS for that selector instead of a global block. See packages/…/nested-at-rule.js• L227-236 and packages/…/ruleset.js• L281-285.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep parent-selector joining aligned with the selector paths used by extend matching. A change in combinator or path shape can make an extension stop matching without a parse error.
  • Preserve the scope stack for both media and container conditions. Changes to condition bubbling must keep same-kind combinations valid and must not move a nested different-kind at-rule outside its parent.
  • Keep rooted at-rule decisions consistent with selector lists that contain both parent references and ordinary selectors. Otherwise output can silently change from selector-scoped CSS to a global conditional block.
  • Keep extension scope propagation aligned with each at-rule visitor. An extension can otherwise affect rules outside its intended media or container condition.

Important failure modes

TriggerConsequenceGuard
A nested selector uses the wrong & positionLess emits a different relationship, such as a descendant instead of a compound selectorCheck the joined selector output for the intended combinator; parent replacement is handled in packages/…/ruleset.js• L969-1005
An extension target does not match the selector pathThe extending selector is absent from the output and Less warns about the unmatched extensionTreat the warning as a selector contract issue; matching rules are in packages/…/extend-visitor.js• L298-373
An extension chain is circularProcessing stops with a circular-reference error instead of producing unbounded selector expansionBreak the cycle or change the extension target; the limit is enforced in packages/…/extend-visitor.js• L225-242
Media choices are nested with a different at-rule kindThe conditions cannot be combined as one same-kind query and remain structurally nestedKeep the required media/container nesting and inspect the emitted at-rule order; see packages/…/nested-at-rule.js• L155-170

Key files

FileWhy to read it
packages/…/ruleset.jsDefines selector path joining and parent-selector replacement.
packages/…/selector.jsDefines selector values, derived selectors, and output behavior.
packages/…/extend.jsDefines the selector extension record and its matching options.
packages/…/extend-visitor.jsFinds extensions, resolves chains, and adds matching selector paths.
packages/…/nested-at-rule.jsDefines condition combination, permutation, and selector bubbling for nestable at-rules.
packages/…/media.jsDefines media condition evaluation and CSS output.
packages/…/container.jsDefines container condition evaluation and CSS output.
packages/…/atrule.jsDefines at-rule scope, rooted output, and boundary handling.
packages/…/join-selector-visitor.jsApplies selector paths and marks at-rule child boundaries before output.
packages/…/selectors.lessShows nested selectors, parent references, interpolation, and selector functions.
packages/…/extend.lessShows extension matching, all, and chained selector results.
packages/…/extend-media.lessShows extension scope across media conditions.
packages/…/media.lessShows nested media composition and selector-scoped media blocks.
packages/…/container.lessShows nested and named container conditions.

Related pages: Less language features, Variables, mixins, and namespaces, and Evaluation and CSS generation.