brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: native css nesting with & removes the need for a preprocessor

Css
~/tips/tip-css-native-nesting-ampersand

.card {

Writes child rules inside the parent rule in plain stylesheets, without Sass or a build step.

Most of my small projects no longer need Sass just to write a nested rule.

.card {
  padding: 1rem;
  & h2 { margin: 0; }
  &:hover { box-shadow: 0 2px 8px #0003; }
  @media (width >= 40rem) { padding: 2rem; }
}

Plain CSS now accepts rules inside other rules. & stands for the parent selector, so &:hover means .card:hover, and media queries can sit right next to the declarations they change. The relaxed syntax, where a nested type selector like h2 needs no &, is in all current browsers since December 2023. Nested rules do add the parent’s specificity, as :is() would, so a deep nest gets heavy quickly.

Further reading: MDN: CSS nesting

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link