Native CSS nesting in WordPress themes without Sass
Many themes still compile Sass for one reason: nesting. Writing the rules of a component inside the component’s selector keeps them together and readable. Browsers can do that natively now, and together with custom properties, which replaced Sass variables long ago, a small or medium theme can drop the build step for its CSS.
Problem: A child theme has a style.scss compiled with sass --watch. The build is the only reason the theme has a package.json, and every quick fix on the server means editing the compiled file or setting up Node.js first.
Solution: Rewrite the stylesheet as plain CSS with native nesting: keep & for pseudo-classes and compound selectors, write BEM elements as full class names because native nesting cannot concatenate strings like &__title, replace $variables with custom properties, and nest media queries inside the rule.
Sass and native CSS side by side
/* Sass (before) */
$color-primary: #1a56db;
.ha-card {
border: 1px solid #e5e7eb;
&__title {
font-size: 1.125rem;
a { color: $color-primary; }
}
&:hover { box-shadow: 0 4px 12px rgb(0 0 0 / .1); }
@media (min-width: 768px) { display: flex; }
}
/* native CSS (after) */
:root {
--ha-color-primary: #1a56db;
}
.ha-card {
border: 1px solid #e5e7eb;
.ha-card__title {
font-size: 1.125rem;
a { color: var(--ha-color-primary); }
}
&:hover { box-shadow: 0 4px 12px rgb(0 0 0 / .1); }
@media (min-width: 768px) {
display: flex;
}
}
The differences that matter:
- No string concatenation. In Sass,
&__titlebuilds the class.ha-card__title. In CSS,&is a selector, not text, so&__titleis not a valid selector and the browser drops the whole rule. Write the full class. &is optional for descendants..ha-card__titleinside.ha-cardmeans.ha-card .ha-card__title. For pseudo-classes and compound selectors the&is required:&:hover,&.is-active.- Media and container queries nest and apply to the parent selector.
- Specificity of a nested rule is calculated as if the parent were wrapped in
:is(). With simple class selectors that changes nothing; with a selector list like.a, #b { .c {} }the nested rule gets the specificity of the ID.
WordPress block classes
.wp-block-button {
.wp-block-button__link {
border-radius: 999px;
transition: background-color .2s;
&:hover,
&:focus-visible {
background-color: var(--wp--preset--color--contrast);
}
}
&.is-style-outline .wp-block-button__link {
border-width: 2px;
}
}
The --wp--preset--* custom properties come from theme.json and are the replacement for a Sass color map. More on them: CSS custom properties in WordPress themes.
What Sass still does that CSS does not
Mixins, loops and functions that generate rules (@each over a color map, darken()) have no direct native counterpart; color-mix() covers many color calculations. If the theme relies on them heavily, keep Sass. If the Sass file is mostly nesting and variables, the switch is a search and replace job of an afternoon. Sass in themes in general: Sass and SCSS in WordPress themes.
Browser support
Native nesting works in Chrome and Edge 112, Safari 16.5 and Firefox 117 (all 2023). The first versions in Chrome and Safari required & or another symbol before a nested element selector (& a instead of a); the relaxed syntax used above came with Chrome 120 and Safari 17.2 (December 2023). For a theme that must work in older browsers, write & a, which works in both versions of the syntax. Browsers without nesting support read a nested rule as an invalid declaration, and the error recovery can swallow the declarations that follow it; put the plain declarations of a rule before its nested rules, as in the examples, and test in the oldest browser you support.
NOTE: The theme still needs style.css with its header comment in the theme folder; the stylesheet itself can be that file or another one enqueued with wp_enqueue_style() and a version number from the theme. If the theme loads its CSS into the block editor with add_editor_style(), check the editor too: WordPress rewrites editor styles to scope them to the editor canvas, and nested rules should be verified there once.