|
| 1 | +# CSS Nesting Conventions |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This project uses **native CSS nesting** (W3C CSS Nesting spec), not Sass/SCSS. They look similar but behave differently in one important way: native CSS does not support BEM-style `&` concatenation for child element selectors. |
| 6 | + |
| 7 | +## The key rule |
| 8 | + |
| 9 | +> `&` must be followed by a combinator (space, `>`, `~`, `+`) or a selector starting with `.`, `#`, `:`, `[`, or `*`. It **cannot** be followed by a bare identifier or `__` prefix. |
| 10 | +
|
| 11 | +## ❌ What NOT to do — Sass-style BEM concatenation |
| 12 | + |
| 13 | +```css |
| 14 | +/* Sass/SCSS — does NOT work in native CSS */ |
| 15 | +.demo-controls { |
| 16 | + padding: 1.6rem; |
| 17 | + |
| 18 | + &__heading { /* ← Invalid native CSS — esbuild converts to :is(__heading) */ |
| 19 | + font-size: 1.1rem; |
| 20 | + } |
| 21 | + |
| 22 | + &__fields { /* ← Invalid native CSS */ |
| 23 | + display: flex; |
| 24 | + } |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +**Why it silently fails**: esbuild converts `&__heading` to `:is(__heading)`, which tries to match an HTML element named `__heading`. No such element exists, so the styles are never applied. There's no error — just missing styles. |
| 29 | + |
| 30 | +The build will show this warning: |
| 31 | +``` |
| 32 | +▲ [WARNING] Cannot use type selector "__heading" directly after nesting selector "&" [css-syntax-error] |
| 33 | + CSS nesting syntax does not allow the "&" selector to come before a type selector. |
| 34 | +``` |
| 35 | + |
| 36 | +## ✅ Correct patterns |
| 37 | + |
| 38 | +### Nested descendant (preferred for BEM child elements) |
| 39 | + |
| 40 | +```css |
| 41 | +.demo-controls { |
| 42 | + padding: 1.6rem; |
| 43 | + |
| 44 | + & .demo-controls__heading { /* space + full class name */ |
| 45 | + font-size: 1.1rem; |
| 46 | + } |
| 47 | + |
| 48 | + & .demo-controls__fields { |
| 49 | + display: flex; |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +### Flat top-level rules (also valid — avoids repetition for deeply nested structures) |
| 55 | + |
| 56 | +```css |
| 57 | +.demo-controls { |
| 58 | + padding: 1.6rem; |
| 59 | +} |
| 60 | + |
| 61 | +.demo-controls__heading { |
| 62 | + font-size: 1.1rem; |
| 63 | +} |
| 64 | + |
| 65 | +.demo-controls__fields { |
| 66 | + display: flex; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### Same-element modifier (this IS valid) |
| 71 | + |
| 72 | +```css |
| 73 | +/* & followed by a class — matches the same element that also has this class */ |
| 74 | +.button { |
| 75 | + background: blue; |
| 76 | + |
| 77 | + &.button--large { /* ← valid: & immediately followed by . */ |
| 78 | + padding: 2rem; |
| 79 | + } |
| 80 | + |
| 81 | + &:hover { /* ← valid: & immediately followed by : */ |
| 82 | + background: darkblue; |
| 83 | + } |
| 84 | + |
| 85 | + &[disabled] { /* ← valid: & immediately followed by [ */ |
| 86 | + opacity: 0.5; |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Pseudo-elements and pseudo-classes |
| 92 | + |
| 93 | +```css |
| 94 | +.component { |
| 95 | + &::before { content: ""; } /* ✅ valid */ |
| 96 | + &::after { content: ""; } /* ✅ valid */ |
| 97 | + &:focus { outline: auto; } /* ✅ valid */ |
| 98 | + &:not(.active) { opacity: 0.5; } /* ✅ valid */ |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Media / container queries inside a rule |
| 103 | + |
| 104 | +```css |
| 105 | +.component { |
| 106 | + grid-template-columns: 1fr; |
| 107 | + |
| 108 | + @media (width >= 768px) { |
| 109 | + grid-template-columns: 1fr 2fr; /* ✅ valid — query wraps the property */ |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Spot-check during review |
| 115 | + |
| 116 | +If you see `&__` or `&-` in a `.vue` `<style lang="css">` block, it's Sass syntax and will not work. Convert it to `& .full-class-name` or lift it to a top-level rule. |
| 117 | + |
| 118 | +## Related |
| 119 | + |
| 120 | +- `CLAUDE.md` → Styling Methodology section |
| 121 | +- `css-grid-max-width-gutters.md` — example of correct native nesting in a grid utility |
0 commit comments