postcss-html
PostCSS HTML Syntax
PostCSS syntax for parsing HTML (and HTML-like)
Getting Started
First thing's first, install the module:
npm install postcss-html --save-dev
postcss-html is an ES module and requires Node.js ^22.12 || >=24. CommonJS consumers on those Node.js versions can still load it with require("postcss-html").
If you want support SCSS/SASS/LESS/SugarSS syntax, you need to install the corresponding module.
- SCSS: postcss-scss
- SASS: postcss-sass
- LESS: postcss-less
- SugarSS: sugarss
- Stylus: postcss-styl
Use Cases
import postcss from 'postcss';
import postcssHtml from 'postcss-html';
import postcssScss from 'postcss-scss';
import postcssLess from 'postcss-less';
import postcssSafeParser from 'postcss-safe-parser';
const syntax = postcssHtml({
// syntax for parse scss (non-required options)
scss: postcssScss,
// syntax for parse less (non-required options)
less: postcssLess,
// syntax for parse css blocks (non-required options)
css: postcssSafeParser,
});
postcss(plugins).process(source, { syntax: syntax }).then(function (result) {
// An alias for the result.css property. Use it with syntaxes that generate non-CSS output.
result.content
});
If you want support SCSS/SASS/LESS/SugarSS syntax, you need to install these module:
- SCSS: postcss-scss
- SASS: postcss-sass
- LESS: postcss-less
- SugarSS: sugarss
- Stylus: postcss-styl
Advanced Use Cases
Options
import postcssHtml from 'postcss-html';
import postcssSass from 'postcss-sass';
import sugarss from 'sugarss';
import postcssCustomSyntax from 'postcss-custom-syntax';
const options = {
rules: [
{
// custom language
test: /^postcss$/i,
lang: 'scss'
},
{
// custom language
test: /^customcss$/i,
lang: 'custom'
},
],
// custom parser for CSS (using `postcss-safe-parser`)
css: 'postcss-safe-parser',
// custom parser for SASS (PostCSS-compatible syntax.)
sass: postcssSass,
// custom parser for SCSS (by module name)
scss: 'postcss-scss',
// custom parser for LESS (by module path)
less: './node_modules/postcss-less',
// custom parser for SugarSS
sugarss: sugarss,
// custom parser for custom language
custom: postcssCustomSyntax,
}
const syntax = postcssHtml(options);
Turning PostCSS off from within your HTML
PostCSS can be temporarily turned off by using special comments in your HTML. For example:
<html>
<body>
<!-- postcss-ignore -->
<a style="color: red;" description="style is not parsed."></a>
<a style="color: red;" description="style is parsed."></a>
<html>
<body>
<!-- postcss-disable -->
<a style="color: red;" description="style is not parsed."></a>
<a style="color: red;" description="style is not parsed."></a>
<!-- postcss-enable -->
<a style="color: red;" description="style is parsed."></a>
Linting with Stylelint
The main use case of this plugin is to apply linting with Stylelint to <style> tags and <div style="*"> property in HTML (and HTML-like).
You can use it by configuring your stylelint config as follows:
{
"overrides": [
{
"files": ["*.html", "**/*.html"],
"customSyntax": "postcss-html"
}
]
}
You can use it more easily if you use an already configured sharable config.
{
+ "extends": [
+ "stylelint-config-html",
// If you are using Vue.
+ "stylelint-config-recommended-vue"
+ ],
- "overrides": [
- {
- "files": ["*.html", "**/*.html"],
- "customSyntax": "postcss-html"
- }
- ]
}
Related Packages
SCSS/CSS starting-point for every project. Mixes smacss namespaced h5bp things w...
Version History
| Version | Released | License |
|---|---|---|
| 2.0.0 | MIT | |
| 1.8.1 | MIT | |
| 1.8.0 | MIT | |
| 1.7.0 | MIT | |
| 1.6.0 | MIT | |
| 1.5.0 | MIT | |
| 1.4.1 | MIT | |
| 1.4.0 | MIT | |
| 1.3.1 | MIT | |
| 1.3.0 | MIT | |
| 1.2.0 | MIT | |
| 1.1.1 | MIT | |
| 1.1.0 | MIT | |
| 1.0.2 | MIT | |
| 1.0.1 | MIT | |
| 1.0.0 | MIT | |
| 0.36.0 | MIT | |
| 0.34.0 | MIT | |
| 0.33.0 | MIT | |
| 0.32.0 | MIT | |
| 0.31.0 | MIT | |
| 0.30.0 | MIT | |
| 0.28.0 | MIT | |
| 0.27.0 | MIT | |
| 0.26.0 | MIT | |
| 0.25.0 | MIT | |
| 0.24.0 | MIT | |
| 0.23.7 | MIT | |
| 0.23.6 | MIT | |
| 0.23.5 | MIT |
Showing the latest 30 of 58. See every release on npm
Pre-releases (3)
| Version | Released | License |
|---|---|---|
| 1.0.0-alpha.4 | MIT | |
| 1.0.0-alpha.3 | MIT | |
| 1.0.0-alpha.2 | MIT |