Skip to content

[css-nesting] support nesting at-rules #2891

@jonathantneal

Description

@jonathantneal

There are situations when nesting at-rules is more intuitive than spreading rule selectors.

Consider the following CSS for a typical website ”top bar”:

.topbar {
  inset: 0 0 auto;
  position: fixed;
}

.topbar-link {
  padding: 15px;
}

@media (width < 1024px) {
  .topbar {
    position: absolute;
  }
}

@media (width < 640px) {
  .topbar {
    position: static;
  }

  .topbar-link {
    padding: 10px;
  }
}

In this example, it is easier to scan at-rules nested within style rules, rather than the other way around. The occasional side-effect of brevity (due to the lack of selector repetition) further improves readability:

.topbar {
  inset: 0 0 auto;
  position: fixed;

  @media (width < 1024px) {
    position: absolute;
  }

  @media (width < 640px) {
    position: static;
  }
}

.topbar-link {
  padding: 15px;

  @media (width < 640px) {
    padding: 10px;
  }
}

As at-rule nesting is such a common feature in CSS Preprocessors, is is often conceptually bundled with “nesting” in general. This is not the case in the current css-nesting proposal, which is why I’ve created this separate issue.

I’m not sure how at-rule nesting would be added to the specs. It may (only?) require updates to css-syntax and css-conditional (citation). Now seems like a good time to discuss this, as similar changes may be needed to support the @nest rule.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions