Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Not a preprocessor - it's a CSS transformer
Uses plugins to add features to CSS
Autoprefixer, minification, and more
Works with regular CSS
Used by major frameworks (Tailwind, Bootstrap)
Faster and more flexible than Sass/Less
💡 Key Difference: Sass/Less add features with new syntax. PostCSS adds features to regular CSS using plugins. You can even use them together!
You write regular CSS
.box { display: flex; }PostCSS reads the CSS
Converts to JavaScript ASTPlugins modify the CSS
Autoprefixer adds prefixesFinal CSS is generated
.box { display: -webkit-flex; }/* INPUT: Your CSS */
.box {
display: flex;
user-select: none;
}
/* ⬇️ PostCSS processes with plugins ⬇️ */
/* OUTPUT: Enhanced CSS */
.box {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}Automatically adds vendor prefixes for browser compatibility
display: flex → display: -webkit-flex; display: flex;Minifies CSS for production (removes whitespace, optimizes)
Reduces file size by 50-70%Use future CSS features today (like Babel for CSS)
Custom properties, nesting, modern color functionsWrite nested CSS like Sass, but in regular CSS
.card { .title { } }Inline @import rules for better performance
Combines multiple CSS files into one# Install PostCSS and plugins
npm install -D postcss postcss-cli autoprefixer
# Or with a config preset
npm install -D postcss postcss-preset-env// postcss.config.js
module.exports = {
plugins: [
// Add vendor prefixes
require('autoprefixer'),
// Use future CSS features
require('postcss-preset-env')({
stage: 2, // Which CSS features to polyfill
features: {
'nesting-rules': true
}
}),
// Minify for production
process.env.NODE_ENV === 'production' ? require('cssnano') : null,
].filter(Boolean)
};// With Vite (built-in support)
// Just create postcss.config.js, Vite will use it automatically!
// With Webpack
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader']
}
]
}
};
// With Next.js (built-in)
// Just create postcss.config.js, Next.js will use it!/* INPUT: Modern CSS */
.box {
display: flex;
user-select: none;
backdrop-filter: blur(10px);
}
/* OUTPUT: With vendor prefixes */
.box {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-backdrop-filter: blur(10px);
backdrop-filter: blur(10px);
}/* INPUT: Nested CSS (Sass-like) */
.card {
padding: 20px;
.card-title {
font-size: 20px;
&:hover {
color: blue;
}
}
}
/* OUTPUT: Flat CSS */
.card {
padding: 20px;
}
.card .card-title {
font-size: 20px;
}
.card .card-title:hover {
color: blue;
}/* INPUT: Future CSS features */
:root {
--primary: #3b82f6;
}
.button {
background: var(--primary);
/* Nesting (future CSS) */
&:hover {
background: color-mod(var(--primary) alpha(90%));
}
/* Custom media queries */
@media (--tablet) {
padding: 16px;
}
}
/* OUTPUT: Current CSS */
:root {
--primary: #3b82f6;
}
.button {
background: var(--primary);
}
.button:hover {
background: rgba(59, 130, 246, 0.9);
}
@media (min-width: 768px) {
.button {
padding: 16px;
}
}// postcss.config.js
const isProduction = process.env.NODE_ENV === 'production';
module.exports = {
plugins: [
// 1. Import CSS files
require('postcss-import'),
// 2. Enable nesting
require('postcss-nested'),
// 3. Use future CSS features
require('postcss-preset-env')({
stage: 2,
features: {
'nesting-rules': true,
'custom-properties': true,
'custom-media-queries': true,
},
autoprefixer: {
grid: true // Enable IE grid support
}
}),
// 4. Add vendor prefixes (included in preset-env)
// Autoprefixer is already in postcss-preset-env
// 5. Minify for production only
isProduction && require('cssnano')({
preset: ['default', {
discardComments: {
removeAll: true,
},
normalizeWhitespace: true,
}]
}),
// 6. Remove unused CSS (optional)
isProduction && require('@fullhuman/postcss-purgecss')({
content: ['./src/**/*.html', './src/**/*.jsx'],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
})
].filter(Boolean)
};Tailwind, Next.js use PostCSS by default
Autoprefixer for vendor prefixes
Use upcoming CSS features now
Minify and optimize for production
Tailwind CSS: Uses PostCSS for processing utility classes
Next.js: Built-in PostCSS support with zero config
Vite: Native PostCSS support, just add config file
Create React App: PostCSS included, can customize