| 1 | # PostCSS Nesting [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][postcss]
|
|---|
| 2 |
|
|---|
| 3 | [![NPM Version][npm-img]][npm-url]
|
|---|
| 4 | [![CSS Standard Status][css-img]][css-url]
|
|---|
| 5 | [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 6 |
|
|---|
| 7 | [PostCSS Nesting] lets you nest style rules inside each other, following the
|
|---|
| 8 | [CSS Nesting] specification. If you want nested rules the same way [Sass] works
|
|---|
| 9 | you might want to use [PostCSS Nested] instead.
|
|---|
| 10 |
|
|---|
| 11 | ```pcss
|
|---|
| 12 | a, b {
|
|---|
| 13 | color: red;
|
|---|
| 14 |
|
|---|
| 15 | /* "&" comes first */
|
|---|
| 16 | & c, & d {
|
|---|
| 17 | color: white;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | /* "&" comes later, requiring "@nest" */
|
|---|
| 21 | @nest e & {
|
|---|
| 22 | color: yellow;
|
|---|
| 23 | }
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | /* becomes */
|
|---|
| 27 |
|
|---|
| 28 | a, b {
|
|---|
| 29 | color: red;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | a c, a d, b c, b d {
|
|---|
| 33 | color: white;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | e a, e b {
|
|---|
| 37 | color: yellow;
|
|---|
| 38 | }
|
|---|
| 39 | ```
|
|---|
| 40 |
|
|---|
| 41 | ## Usage
|
|---|
| 42 |
|
|---|
| 43 | Add [PostCSS Nesting] to your project:
|
|---|
| 44 |
|
|---|
| 45 | ```bash
|
|---|
| 46 | npm install postcss-nesting --save-dev
|
|---|
| 47 | ```
|
|---|
| 48 |
|
|---|
| 49 | Use [PostCSS Nesting] as a [PostCSS] plugin:
|
|---|
| 50 |
|
|---|
| 51 | ```js
|
|---|
| 52 | import postcss from 'postcss';
|
|---|
| 53 | import postcssNesting from 'postcss-nesting';
|
|---|
| 54 |
|
|---|
| 55 | postcss([
|
|---|
| 56 | postcssNesting(/* pluginOptions */)
|
|---|
| 57 | ]).process(YOUR_CSS /*, processOptions */);
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | [PostCSS Nesting] runs in all Node environments, with special instructions for:
|
|---|
| 61 |
|
|---|
| 62 | | [Node](INSTALL.md#node) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
|
|---|
| 63 | | --- | --- | --- | --- | --- |
|
|---|
| 64 |
|
|---|
| 65 | ### Deno
|
|---|
| 66 |
|
|---|
| 67 | You can also use [PostCSS Nesting] on [Deno]:
|
|---|
| 68 |
|
|---|
| 69 | ```js
|
|---|
| 70 | import postcss from "https://deno.land/x/postcss/mod.js";
|
|---|
| 71 | import postcssNesting from "https://cdn.jsdelivr.net/npm/postcss-nesting@10/mod.js";
|
|---|
| 72 |
|
|---|
| 73 | await postcss([postcssNesting]).process(YOUR_CSS /*, processOptions */);
|
|---|
| 74 | ```
|
|---|
| 75 |
|
|---|
| 76 | ## Options
|
|---|
| 77 |
|
|---|
| 78 | ### noIsPseudoSelector
|
|---|
| 79 |
|
|---|
| 80 | #### Specificity
|
|---|
| 81 |
|
|---|
| 82 | Before :
|
|---|
| 83 |
|
|---|
| 84 | ```css
|
|---|
| 85 | #alpha,
|
|---|
| 86 | .beta {
|
|---|
| 87 | &:hover {
|
|---|
| 88 | order: 1;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | ```
|
|---|
| 92 |
|
|---|
| 93 | After **without** the option :
|
|---|
| 94 |
|
|---|
| 95 | ```js
|
|---|
| 96 | postcssNesting()
|
|---|
| 97 | ```
|
|---|
| 98 |
|
|---|
| 99 | ```css
|
|---|
| 100 | :is(#alpha,.beta):hover {
|
|---|
| 101 | order: 1;
|
|---|
| 102 | }
|
|---|
| 103 | ```
|
|---|
| 104 |
|
|---|
| 105 | _`.beta:hover` has specificity as if `.beta` where an id selector, matching the specification._
|
|---|
| 106 |
|
|---|
| 107 | [specificity: 1, 1, 0](https://polypane.app/css-specificity-calculator/#selector=%3Ais(%23alpha%2C.beta)%3Ahover)
|
|---|
| 108 |
|
|---|
| 109 | After **with** the option :
|
|---|
| 110 |
|
|---|
| 111 | ```js
|
|---|
| 112 | postcssNesting({
|
|---|
| 113 | noIsPseudoSelector: true
|
|---|
| 114 | })
|
|---|
| 115 | ```
|
|---|
| 116 |
|
|---|
| 117 | ```css
|
|---|
| 118 | #alpha:hover, .beta:hover {
|
|---|
| 119 | order: 1;
|
|---|
| 120 | }
|
|---|
| 121 | ```
|
|---|
| 122 |
|
|---|
| 123 | _`.beta:hover` has specificity as if `.beta` where a class selector, conflicting with the specification._
|
|---|
| 124 |
|
|---|
| 125 | [specificity: 0, 2, 0](https://polypane.app/css-specificity-calculator/#selector=.beta%3Ahover)
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | #### Complex selectors
|
|---|
| 129 |
|
|---|
| 130 | Before :
|
|---|
| 131 |
|
|---|
| 132 | ```css
|
|---|
| 133 | .alpha > .beta {
|
|---|
| 134 | & + & {
|
|---|
| 135 | order: 2;
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | ```
|
|---|
| 139 |
|
|---|
| 140 | After **without** the option :
|
|---|
| 141 |
|
|---|
| 142 | ```js
|
|---|
| 143 | postcssNesting()
|
|---|
| 144 | ```
|
|---|
| 145 |
|
|---|
| 146 | ```css
|
|---|
| 147 | :is(.alpha > .beta) + :is(.alpha > .beta) {
|
|---|
| 148 | order: 2;
|
|---|
| 149 | }
|
|---|
| 150 | ```
|
|---|
| 151 |
|
|---|
| 152 | After **with** the option :
|
|---|
| 153 |
|
|---|
| 154 | ```js
|
|---|
| 155 | postcssNesting({
|
|---|
| 156 | noIsPseudoSelector: true
|
|---|
| 157 | })
|
|---|
| 158 | ```
|
|---|
| 159 |
|
|---|
| 160 | ```css
|
|---|
| 161 | .alpha > .beta + .alpha > .beta {
|
|---|
| 162 | order: 2;
|
|---|
| 163 | }
|
|---|
| 164 | ```
|
|---|
| 165 |
|
|---|
| 166 | _this is a different selector than expected as `.beta + .alpha` matches `.beta` followed by `.alpha`._<br>
|
|---|
| 167 | _avoid these cases when you disable `:is()`_<br>
|
|---|
| 168 | _writing the selector without nesting is advised here_
|
|---|
| 169 |
|
|---|
| 170 | ```css
|
|---|
| 171 | /* without nesting */
|
|---|
| 172 | .alpha > .beta + .beta {
|
|---|
| 173 | order: 2;
|
|---|
| 174 | }
|
|---|
| 175 | ```
|
|---|
| 176 |
|
|---|
| 177 | ### ⚠️ Spec disclaimer
|
|---|
| 178 |
|
|---|
| 179 | The [CSS Nesting Module] spec states on nesting that "Declarations occurring after a nested rule are invalid and ignored.".
|
|---|
| 180 | While we think it makes sense on browsers, enforcing this at the plugin level introduces several constraints that would
|
|---|
| 181 | interfere with PostCSS' plugin nature such as with `@mixin`
|
|---|
| 182 |
|
|---|
| 183 | [css-img]: https://cssdb.org/images/badges/nesting-rules.svg
|
|---|
| 184 | [css-url]: https://cssdb.org/#nesting-rules
|
|---|
| 185 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 186 | [npm-img]: https://img.shields.io/npm/v/postcss-nesting.svg
|
|---|
| 187 | [npm-url]: https://www.npmjs.com/package/postcss-nesting
|
|---|
| 188 |
|
|---|
| 189 | [CSS Nesting]: https://drafts.csswg.org/css-nesting-1/
|
|---|
| 190 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 191 | [PostCSS Nesting]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-nesting
|
|---|
| 192 | [Deno]: https://deno.land/x/postcss_nesting
|
|---|
| 193 | [PostCSS Nested]: https://github.com/postcss/postcss-nested
|
|---|
| 194 | [Sass]: https://sass-lang.com/
|
|---|
| 195 | [CSS Nesting Module]: https://www.w3.org/TR/css-nesting-1/
|
|---|