CSS Nesting Selectors (&)
- Previous Page [attribute*=value]
- Next Page element
- Go to the Previous Level CSS Selector Reference Manual
Definition and Usage
CSS Nesting (&
) Selectors are used to apply styles to an element within the context of another element.
Nesting reduces the need to repeat selectors for related elements.
Example
Example 1
Before nesting, you must explicitly declare each selector separately, as shown below:
.box { border: 2px solid green; background-color: beige; font-family: monospace; font-size: 20px; } .box > a { color: green; } .box > a:hover { color: white; background-color: salmon; }
Example 2
After nesting, the selector is extended, and the related style rules are grouped within the parent rule:
.box { border: 2px solid green; background-color: beige; font-family: monospace; font-size: 20px; & > a { color: green; &:hover { color: white; background-color: salmon; } } }
Tip:If the .box style in the above example should be removed from your project, you can remove the entire group without searching for related selectors.
CSS Syntax
parentrule { css declarations; & childrule { css declarations; } }
Technical Details
Version: | CSS Nesting Module |
---|
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
120 | 120 | 117 | 17.2 | 106 |
- Previous Page [attribute*=value]
- Next Page element
- Go to the Previous Level CSS Selector Reference Manual