You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the first example we had a rule set that began with the selector `#questions .button`, which would match any widget with a class called "button" that is inside a container with id `questions`.
528
+
529
+
In the second example, the button rule selector is simply `.button`, but it is *within* the rule set with selector `#questions`.
530
+
The nesting means that the button rule set will inherit the selector from the outer rule set, so it is equivalent to `#questions .button`.
531
+
532
+
### Nesting selector
533
+
534
+
The two remaining rules are nested within the button rule, which means they will inherit their selectors from the button rule set *and* the outer `#questions` rule set.
535
+
536
+
You may have noticed that the rules for the button styles contain a syntax we haven't seen before.
537
+
The rule for the Yes button is `&.affirmative`.
538
+
The ampersand (`&`) is known as the *nesting selector* and it tells Textual that the selector should be combined with the selector from the outer rule set.
539
+
540
+
So `&.affirmative` in the example above, produces the equivalent of `#questions .button.affirmative` which selects a widget with both the `button` and `affirmative` classes.
541
+
Without `&` it would be equivalent to `#questions .button .affirmative` (note the additional space) which would only match a widget with class `affirmative` inside a container with class `button`.
542
+
543
+
544
+
For reference, lets see those two CSS files side-by-side:
545
+
546
+
=== "nesting01.tcss"
547
+
548
+
```css
549
+
--8<-- "docs/examples/guide/css/nesting01.tcss"
550
+
```
551
+
552
+
=== "nesting02.tcss"
553
+
554
+
```sass
555
+
--8<-- "docs/examples/guide/css/nesting02.tcss"
556
+
```
557
+
558
+
### Why use nesting?
559
+
560
+
There is no requirement to use nested CSS, but it can help to group related rule sets together (which makes it easier to edit). Nested CSS can also help you avoid some repetition in your selectors, i.e. in the nested CSS we only need to type `#questions` once, rather than four times in the non-nested CSS.
0 commit comments