| 1 | # PostCSS Nested
|
|---|
| 2 |
|
|---|
| 3 | <img align="right" width="135" height="95"
|
|---|
| 4 | title="Philosopher’s stone, logo of PostCSS"
|
|---|
| 5 | src="https://postcss.org/logo-leftp.svg">
|
|---|
| 6 |
|
|---|
| 7 | [PostCSS] plugin to unwrap nested rules closer to Sass syntax.
|
|---|
| 8 |
|
|---|
| 9 | ```css
|
|---|
| 10 | .phone {
|
|---|
| 11 | &_title {
|
|---|
| 12 | width: 500px;
|
|---|
| 13 | @media (max-width: 500px) {
|
|---|
| 14 | width: auto;
|
|---|
| 15 | }
|
|---|
| 16 | body.is_dark & {
|
|---|
| 17 | color: white;
|
|---|
| 18 | }
|
|---|
| 19 | }
|
|---|
| 20 | img {
|
|---|
| 21 | display: block;
|
|---|
| 22 | }
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | .title {
|
|---|
| 26 | font-size: var(--font);
|
|---|
| 27 |
|
|---|
| 28 | @at-root html {
|
|---|
| 29 | --font: 16px;
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 | ```
|
|---|
| 33 |
|
|---|
| 34 | will be processed to:
|
|---|
| 35 |
|
|---|
| 36 | ```css
|
|---|
| 37 | .phone_title {
|
|---|
| 38 | width: 500px;
|
|---|
| 39 | }
|
|---|
| 40 | @media (max-width: 500px) {
|
|---|
| 41 | .phone_title {
|
|---|
| 42 | width: auto;
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|
| 45 | body.is_dark .phone_title {
|
|---|
| 46 | color: white;
|
|---|
| 47 | }
|
|---|
| 48 | .phone img {
|
|---|
| 49 | display: block;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | .title {
|
|---|
| 53 | font-size: var(--font);
|
|---|
| 54 | }
|
|---|
| 55 | html {
|
|---|
| 56 | --font: 16px;
|
|---|
| 57 | }
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | Related plugins:
|
|---|
| 61 |
|
|---|
| 62 | - Use [`postcss-current-selector`] **after** this plugin if you want
|
|---|
| 63 | to use current selector in properties or variables values.
|
|---|
| 64 | - Use [`postcss-nested-ancestors`] **before** this plugin if you want
|
|---|
| 65 | to reference any ancestor element directly in your selectors with `^&`.
|
|---|
| 66 |
|
|---|
| 67 | Alternatives:
|
|---|
| 68 |
|
|---|
| 69 | - See also [`postcss-nesting`], which implements [CSSWG draft].
|
|---|
| 70 | - [`postcss-nested-props`] for nested properties like `font-size`.
|
|---|
| 71 |
|
|---|
| 72 | <a href="https://evilmartians.com/?utm_source=postcss-nested">
|
|---|
| 73 | <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
|
|---|
| 74 | alt="Sponsored by Evil Martians" width="236" height="54">
|
|---|
| 75 | </a>
|
|---|
| 76 |
|
|---|
| 77 | [`postcss-current-selector`]: https://github.com/komlev/postcss-current-selector
|
|---|
| 78 | [`postcss-nested-ancestors`]: https://github.com/toomuchdesign/postcss-nested-ancestors
|
|---|
| 79 | [`postcss-nested-props`]: https://github.com/jedmao/postcss-nested-props
|
|---|
| 80 | [`postcss-nesting`]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-nesting
|
|---|
| 81 | [CSSWG draft]: https://drafts.csswg.org/css-nesting-1/
|
|---|
| 82 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 83 |
|
|---|
| 84 | ## Docs
|
|---|
| 85 | Read full docs **[here](https://github.com/postcss/postcss-nested#readme)**.
|
|---|