CSS Preprocessors
In this page:
Preprocessors vs. Native CSS
CSS Preprocessors (like Sass, Less, or Stylus) are scripting languages that extend CSS with advanced features like nesting, variables, and mixins. You write your code in preprocessor syntax, which is compiled into standard CSS that browsers can read.
Example: Preprocessors vs. Native CSS
<!-- SCSS source (needs a preprocessor):
$color: teal;
p {
color: $color;
}
-->
<style>
p { color: teal; }
</style>
<p>Compiled output: teal text</p>
Preprocessor Nesting
Preprocessors allow you to nest your CSS selectors inside each other, matching the nesting structure of your HTML tags, keeping your code organized and reducing repetitive selector paths.
Example: Preprocessor Nesting
<style>
.card .title { font-weight: bold; }
.card { border: 2px solid #1e6fd9; padding: 12px; width: 200px; }
.card {
.title {
font-weight: bold;
}
}
</style>
<!-- Nested SCSS compiled to plain CSS below -->
<div class="card"><p class="title">Card title</p></div>
Preprocessor Mixins
Mixins are reusable blocks of CSS code that you can define once and include inside other selectors, preventing repetitive style rules and keeping your stylesheets DRY (Don't Repeat Yourself).
Example: Preprocessor Mixins
<!-- SCSS source (needs a preprocessor):
@mixin button($bg) {
background: $bg;
padding: 10px 20px;
}
.btn {
@include button(royalblue);
}
-->
<style>
.btn { background: royalblue; padding: 10px 20px; border: 0; color: white; }
</style>
<button class="btn">Button</button>
Preprocessor Variables
Preprocessors were the first to introduce variables to CSS. Preprocessor variables are static; they are replaced with their actual values during compilation and cannot be updated dynamically inside the browser's DOM like native CSS variables.
Example: Preprocessor Variables
<!-- SCSS source (needs a preprocessor):
$spacing: 16px;
.box {
padding: $spacing;
}
-->
<style>
.box { padding: 16px; background: #cfe8ff; border: 2px solid #1e6fd9; }
</style>
<div class="box">Padding from a variable</div>
Preprocessor File Imports
Preprocessors allow you to split your stylesheets into separate, modular files (like _buttons.scss or _theme.scss) and import them into a single, main stylesheet file using the @use or @import rules, keeping your project organized and easy to maintain.
Example: Preprocessor File Imports
<!-- SCSS source (needs a preprocessor):
@use 'buttons';
@use 'theme';
-->
<style>
.btn { background: royalblue; color: white; border: 0; padding: 10px 20px; }
</style>
<button class="btn">Styles from the imported files</button>
- Deeply nesting selectors past three levels, resulting in bloated, overly specific compiled CSS files.
- Attempting to read or modify preprocessor variables dynamically inside the browser's DOM using JavaScript.
- Forgetting to compile your preprocessor files, leaving browsers to load raw .scss files that they cannot read.
- CSS Preprocessors (like Sass) extend CSS with programming-like features, compiling them into standard CSS.
- Use nesting to organize styles, variables for static configurations, and mixins to reuse blocks of style code cleanly.
- Split your project stylesheets into modular, partial files starting with underscores, and combine them using @use.
Browsers cannot read preprocessor files directly; they must always be compiled into standard CSS files first, which are supported natively by all browsers.
Chapter Quiz — Complete all 25 topics to unlock
0/25 topics done
Complete these topics first:
- CSS Container Queries
- CSS Subgrid
- CSS Logical Properties
- CSS Logical Sizing
- CSS Writing Modes
- CSS Aspect Ratio
- CSS Object Fit
- CSS object-position
- CSS Masking
- CSS Math Functions
- CSS Motion Path
- CSS @supports
- CSS @property
- CSS At-Rules
- CSS Cursor
- CSS Will Change
- CSS User Interface
- CSS Print Styles
- CSS Pagination (Print)
- CSS Dark Mode
- CSS Accessibility
- CSS Performance
- CSS Preprocessors
- CSS Frameworks Overview
- CSS Interview Prep