Lesson 41 of 50
cssPostCSS & Autoprefixer
7 mins
Course progress82%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 41 of 50
css7 mins
Keep these ideas in mind as you work through the lesson.
PostCSS is a plug‑in ecosystem that transforms CSS with JavaScript. It powers Autoprefixer, cssnano, and future‑syntax polyfills.
/* input */
.flex { display:flex; }
# after autoprefixer (via postcss-cli)
.flex { display:-webkit-box; display:-ms-flexbox; display:flex; }
npm install postcss postcss-cli autoprefixer postcss-preset-env --save-dev
Create postcss.config.js:
module.exports = {
plugins: {
autoprefixer: {},
'postcss-preset-env': { stage: 1 }, // enables future CSS (e.g., nesting)
cssnano: { preset: 'default' } // minify for production
}
};
Run via npm script:
"scripts": { "build:css": "postcss src/*.css -d dist/" }
postcss-preset-env:focus-visible or color-mod(), and have it transpiled for older browsers based on your browserslist configuration./* src/components.css */
.card {
padding:1rem;
.title { font-size:1.25rem; }
&:hover { background:#f0f0f0; }
}
After postcss-preset-env →
.card { padding:1rem; }
.card .title { font-size:1.25rem; }
.card:hover { background:#f0f0f0; }
Create a .browserslistrc file:
> 0.5%
last 2 versions
not dead
Run linting before PostCSS so you catch errors early:
"scripts": { "lint:css": "stylelint "src/**/*.css"", "build:css": "npm run lint:css && postcss src/*.css -d dist/" }
cssnano minifies the final bundle, removing comments, whitespace, and redundant rules – essential for production.Autoprefixer does not add accessibility‑related properties, so you must still ensure things like focus-visible are present.