postcss-nesting
Nest rules inside each other in CSS
Last updated 3 years ago by alaguna .
CC0-1.0 · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install postcss-nesting 
SYNC missed versions from official npm registry.

PostCSS Nesting PostCSS Logo

npm version CSS Standard Status Build Status Discord

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;
	}
}

/* becomes */

.foo {
	color: red;
}
.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 first warn and then error if you use @nest.

We advice everyone to migrate their codebase now to nested CSS without @nest.

⚠️ Nested selectors must start with a symbol

The current version of the CSS Nesting specification disallows nested selectors to start with a letter (i.e. a tag name or element selector). To write such selectors, they need to be prefixed with & or wrapped with :is().

You will get a warning when selectors start with a letter:

Nested selectors must start with a symbol and "span" begins with a letter.

.foo {
	/* ❌ invalid */
	span {
		color: hotpink;
	}

	/* ✅ valid */
	& span {
		color: hotpink;
	}

	/* ❌ invalid */
	span & {
		color: hotpink;
	}

	/* ✅ valid */
	:is(span) & {
		color: hotpink;
	}
}	

Options

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

  • 11.2.2                                ...           latest (2 years ago)

48 Versions

  • 11.2.2                                ...           2 years ago
  • 11.2.1                                ...           2 years ago
  • 11.2.0                                ...           2 years ago
  • 11.1.0                                ...           2 years ago
  • 11.0.1                                ...           2 years ago
  • 11.0.0                                ...           2 years ago
  • 10.2.0                                ...           2 years ago
  • 10.1.10                                ...           2 years ago
  • 10.1.9                                ...           2 years ago
  • 10.1.8                                ...           3 years ago
  • 10.1.7                                ...           3 years ago
  • 10.1.6                                ...           3 years ago
  • 10.1.5                                ...           3 years ago
  • 10.1.4                                ...           3 years ago
  • 10.1.3                                ...           3 years ago
  • 10.1.2                                ...           3 years ago
  • 10.1.1                                ...           3 years ago
  • 10.1.0                                ...           3 years ago
  • 10.0.3                                ...           3 years ago
  • 10.0.2                                ...           3 years ago
  • 10.0.1                                ...           3 years ago
  • 10.0.0                                ...           3 years ago
  • 9.0.0                                ...           3 years ago
  • 8.0.1                                ...           4 years ago
  • 8.0.0                                ...           4 years ago
  • 7.0.1                                ...           5 years ago
  • 7.0.0                                ...           6 years ago
  • 6.0.0                                ...           7 years ago
  • 5.0.0                                ...           7 years ago
  • 4.2.1                                ...           7 years ago
  • 4.2.0                                ...           7 years ago
  • 4.1.0                                ...           7 years ago
  • 4.0.1                                ...           8 years ago
  • 4.0.0                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 2.3.1                                ...           9 years ago
  • 2.3.0                                ...           9 years ago
  • 2.2.0                                ...           9 years ago
  • 2.1.1                                ...           9 years ago
  • 2.0.6                                ...           9 years ago
  • 2.0.5                                ...           9 years ago
  • 2.0.4                                ...           9 years ago
  • 2.0.3                                ...           9 years ago
  • 2.0.2                                ...           9 years ago
  • 2.0.1                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
  • 0.1.0                                ...           9 years ago
Downloads
Today 0
This Week 0
This Month 24
Last Day 0
Last Week 24
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None

Copyright 2013 - present © cnpmjs.org | Home |