postcss-nesting
Nest style and media rules inside each another, following the CSS Nesting Module Level 3 specification
Last updated 10 years ago by jonathantneal .
CC0-1.0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install postcss-nesting 
SYNC missed versions from official npm registry.

PostCSS Nesting PostCSS Logo

npm version Build Status Discord

Baseline Status CSS Standard Status

npm install postcss-nesting --save-dev

PostCSS Nesting lets you nest style rules inside each other, following the CSS Nesting specification.

If you want nested rules the same way Sass works you might want to use PostCSS Nested instead.

.foo {
	color: red;

	&:hover {
		color: green;
	}

	> .bar {
		color: blue;
	}

	@media (prefers-color-scheme: dark) {
		color: cyan;
	}

	color: pink;
}

/* becomes */

.foo {
	color: red;

	color: pink;
}
.foo:hover {
		color: green;
	}
.foo > .bar {
		color: blue;
	}
@media (prefers-color-scheme: dark) {
	.foo {
		color: cyan;
}
	}

Usage

Add PostCSS Nesting to your project:

npm install postcss postcss-nesting --save-dev

Use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssNesting = require('postcss-nesting');

postcss([
	postcssNesting(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);

PostCSS Nesting runs in all Node environments, with special instructions for:

⚠️ @nest has been removed from the specification.

Previous iterations of the CSS Nesting specification required using @nest for certain selectors.

@nest was removed from the specification completely. Future versions of this plugin will error if you use @nest.

We advice everyone to migrate their codebase now to nested CSS without @nest.
We published a Stylelint Plugin to help you migrate.

example warning:

@nest was removed from the CSS Nesting specification and will be removed from PostCSS Nesting in the next major version. Change @nest foo & {} to foo & {} to migrate to the latest standard.

You can silence this warning with a new silenceAtNestWarning plugin option.

postcssNesting({
	silenceAtNestWarning: true
})

Options

edition

The default behavior is to transpile CSS following an older version of the CSS nesting specification.

If you want to already use the latest version you can set the edition option to 2024-02.

postcssNesting({
	edition: '2024-02'
})

2021 (default)

This version is a continuation of what existed before CSS nesting was implemented in browsers.
It made a few non-invasive changes to keep up with implementations but it is falling behind.

In a future version of this plugin this will no longer be the default.

.foo {
	color: red;

	&:hover {
		color: green;
	}

	> .bar {
		color: blue;
	}

	@media (prefers-color-scheme: dark) {
		color: cyan;
	}

	color: pink;
}

/* becomes */

.foo {
	color: red;

	color: pink;
}
.foo:hover {
		color: green;
	}
.foo > .bar {
		color: blue;
	}
@media (prefers-color-scheme: dark) {
	.foo {
		color: cyan;
}
	}

2024-02

  • usage of :is() pseudo-class is no longer optional
  • at rules are not combined with the and keyword
  • @nest is removed from the specification
  • declarations and nested rules/at-rules are no longer re-ordered
.foo {
	color: red;

	&:hover {
		color: green;
	}

	> .bar {
		color: blue;
	}

	@media (prefers-color-scheme: dark) {
		color: cyan;
	}

	color: pink;
}

/* becomes */

.foo {
	color: red;
}
.foo:hover {
		color: green;
	}
.foo  > .bar {
		color: blue;
	}
@media (prefers-color-scheme: dark) {
	.foo {
		color: cyan;
}
	}
.foo {

	color: pink;
}

noIsPseudoSelector

Specificity

Before :

#alpha,
.beta {
	&:hover {
		order: 1;
	}
}

After without the option :

postcssNesting()
:is(#alpha,.beta):hover {
	order: 1;
}

.beta:hover has specificity as if .beta where an id selector, matching the specification.

specificity: 1, 1, 0

After with the option :

postcssNesting({
	noIsPseudoSelector: true
})
#alpha:hover, .beta:hover {
	order: 1;
}

.beta:hover has specificity as if .beta where a class selector, conflicting with the specification.

specificity: 0, 2, 0

Complex selectors

Before :

.alpha > .beta {
	& + & {
		order: 2;
	}
}

After without the option :

postcssNesting()
:is(.alpha > .beta) + :is(.alpha > .beta) {
	order: 2;
}

After with the option :

postcssNesting({
	noIsPseudoSelector: true
})
.alpha > .beta + .alpha > .beta {
	order: 2;
}

this is a different selector than expected as .beta + .alpha matches .beta followed by .alpha.
avoid these cases when you disable :is()
writing the selector without nesting is advised here

/* without nesting */
.alpha > .beta + .beta {
	order: 2;
}

Current Tags

  • 14.0.3                                ...           latest (2 months ago)

67 Versions

  • 14.0.3                                ...           2 months ago
  • 14.0.2                                ...           2 months ago
  • 14.0.1                                ...           4 months ago
  • 14.0.0                                ...           10 months ago
  • 13.0.2                                ...           a year ago
  • 13.0.1                                ...           2 years ago
  • 13.0.0                                ...           2 years ago
  • 12.1.5                                ...           3 years ago
  • 12.1.4                                ...           3 years ago
  • 12.1.3                                ...           3 years ago
  • 12.1.2                                ...           3 years ago
  • 12.1.1                                ...           3 years ago
  • 12.1.0                                ...           3 years ago
  • 12.0.4                                ...           3 years ago
  • 12.0.3                                ...           3 years ago
  • 12.0.2                                ...           3 years ago
  • 12.0.1                                ...           3 years ago
  • 12.0.0                                ...           3 years ago
  • 11.3.0                                ...           3 years ago
  • 11.2.2                                ...           4 years ago
  • 11.2.1                                ...           4 years ago
  • 11.2.0                                ...           4 years ago
  • 11.1.0                                ...           4 years ago
  • 11.0.1                                ...           4 years ago
  • 11.0.0                                ...           4 years ago
  • 10.2.0                                ...           4 years ago
  • 10.1.10                                ...           4 years ago
  • 10.1.9                                ...           4 years ago
  • 10.1.8                                ...           4 years ago
  • 10.1.7                                ...           5 years ago
  • 10.1.6                                ...           5 years ago
  • 10.1.5                                ...           5 years ago
  • 10.1.4                                ...           5 years ago
  • 10.1.3                                ...           5 years ago
  • 10.1.2                                ...           5 years ago
  • 10.1.1                                ...           5 years ago
  • 10.1.0                                ...           5 years ago
  • 10.0.3                                ...           5 years ago
  • 10.0.2                                ...           5 years ago
  • 10.0.1                                ...           5 years ago
  • 10.0.0                                ...           5 years ago
  • 9.0.0                                ...           5 years ago
  • 8.0.1                                ...           6 years ago
  • 8.0.0                                ...           6 years ago
  • 7.0.1                                ...           7 years ago
  • 7.0.0                                ...           8 years ago
  • 6.0.0                                ...           8 years ago
  • 5.0.0                                ...           9 years ago
  • 4.2.1                                ...           9 years ago
  • 4.2.0                                ...           9 years ago
  • 4.1.0                                ...           9 years ago
  • 4.0.1                                ...           10 years ago
  • 4.0.0                                ...           10 years ago
  • 3.0.0                                ...           10 years ago
  • 2.3.1                                ...           11 years ago
  • 2.3.0                                ...           11 years ago
  • 2.2.0                                ...           11 years ago
  • 2.1.1                                ...           11 years ago
  • 2.0.6                                ...           11 years ago
  • 2.0.5                                ...           11 years ago
  • 2.0.4                                ...           11 years ago
  • 2.0.3                                ...           11 years ago
  • 2.0.2                                ...           11 years ago
  • 2.0.1                                ...           11 years ago
  • 2.0.0                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 155
Today 0
This Week 0
This Month 82
Last Day 0
Last Week 0
Last Month 71
Dependencies (1)
Dev Dependencies (4)

© 2010 - cnpmjs.org x YWFE | Home | YWFE