Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
SASS with PostCSS Pipeline
📖 Scenario: You are building a simple style setup for a website. You want to write your styles using SASS to use variables and nesting. Then, you want to process your SASS output with PostCSS to add vendor prefixes automatically for better browser support.
🎯 Goal: Create a SASS file with variables and nested styles. Then configure a PostCSS pipeline that uses autoprefixer to add vendor prefixes. Finally, produce a CSS file that is ready to use in a browser.
📋 What You'll Learn
Create a SASS file with a color variable and nested selectors
Create a PostCSS configuration file that uses autoprefixer plugin
Write a script or command to compile SASS to CSS and then run PostCSS on the CSS output
Ensure the final CSS file contains vendor prefixes added by autoprefixer
💡 Why This Matters
🌍 Real World
Web developers often write styles in SASS for easier maintenance and use PostCSS to ensure CSS works well across different browsers by adding necessary prefixes.
💼 Career
Understanding how to set up a SASS and PostCSS pipeline is essential for front-end developers to produce modern, maintainable, and compatible CSS for websites.
Progress0 / 4 steps
1
Create a SASS file with variables and nesting
Create a file named styles.scss. Inside it, define a SASS variable called $primary-color with the value #3498db. Then write a nested style for a .button class that sets background-color to $primary-color and inside it a nested :hover selector that changes background-color to #2980b9.
SASS
Hint
Use $primary-color: #3498db; to define the variable. Nest the :hover selector inside .button using &:hover.
2
Create PostCSS configuration with autoprefixer
Create a file named postcss.config.js. Inside it, export a configuration object that includes the autoprefixer plugin. Use require('autoprefixer') to import the plugin.
SASS
Hint
Use module.exports = { plugins: [ require('autoprefixer') ] }; to configure PostCSS.
3
Write a script to compile SASS and run PostCSS
Write a command or script line that compiles styles.scss to styles.css using the sass CLI, then runs postcss on styles.css to output to styles.prefixed.css. Use the commands sass styles.scss styles.css and postcss styles.css -o styles.prefixed.css chained with &&.
SASS
Hint
Use sass styles.scss styles.css && postcss styles.css -o styles.prefixed.css to compile and process CSS.
4
Verify final CSS contains vendor prefixes
Open the generated styles.prefixed.css file and add a CSS rule for .button with display: flex;. Then run the PostCSS command again to add vendor prefixes like -webkit-flex. Add the display: flex; rule inside the .button selector in styles.scss before compiling and processing again.
SASS
Hint
Add display: flex; inside the .button selector in styles.scss to see vendor prefixes after processing.
Practice
(1/5)
1. What is the main role of PostCSS in a SASS with PostCSS pipeline?
easy
A. To compile SASS code into CSS
B. To process compiled CSS and add browser prefixes automatically
C. To write variables and nesting in styles
D. To minify JavaScript files
Solution
Step 1: Understand the role of SASS
SASS is a preprocessor that lets you write CSS with variables and nesting, but it does not add browser prefixes.
Step 2: Understand the role of PostCSS
PostCSS processes the compiled CSS to add features like browser prefixes automatically, improving browser compatibility.
Final Answer:
To process compiled CSS and add browser prefixes automatically -> Option B
Quick Check:
PostCSS adds prefixes after SASS compiles CSS [OK]
Hint: PostCSS works on CSS output, not on SASS source [OK]
Common Mistakes:
Confusing SASS compilation with PostCSS processing
Thinking PostCSS compiles SASS
Assuming PostCSS writes variables
2. Which of the following is the correct order to use SASS and PostCSS in a build pipeline?
easy
A. Compile SASS first, then run PostCSS
B. Only run SASS, PostCSS is optional
C. Run both simultaneously
D. Run PostCSS first, then compile SASS
Solution
Step 1: Identify the output of SASS
SASS compiles .scss files into plain CSS files.
Step 2: Understand PostCSS input requirements
PostCSS works on CSS files, so it must run after SASS compilation.
Final Answer:
Compile SASS first, then run PostCSS -> Option A
Quick Check:
SASS compiles, PostCSS processes CSS [OK]
Hint: Compile SASS before PostCSS to process CSS output [OK]
Common Mistakes:
Running PostCSS before SASS compilation
Trying to run both at the same time
Skipping PostCSS thinking it's unnecessary
3. Given this SASS code and PostCSS with autoprefixer, what will be the final CSS output?