Example 1 — Theme variables
$brand: #2563eb;
$text: #1e293b;
body {
color: $text;
background: #f8fafc;
}
a { color: $brand; }
This page is a self-contained introduction to Sass—a CSS preprocessor that helps you write cleaner, reusable stylesheets. You will understand what Sass adds to CSS and see the core features with examples.
Preprocessor basics.
SCSS vs Sass.
Variables & more.
DRY & scale.
File structure.
Hands-on SCSS.
Sass (Syntactically Awesome Style Sheets) is a powerful preprocessor scripting language that extends CSS (Cascading Style Sheets). By using Sass, you can write more maintainable, reusable, and flexible stylesheets.
Sass adds features such as variables, nested rules, mixins, functions, and inheritance, which streamline and enhance your CSS workflow. You author SCSS, compile once, and ship plain CSS to the browser.
Learn CSS selectors and the box model first. Sass makes CSS easier to organize—it does not remove the need to understand how styles apply in the browser.
Sass is a CSS preprocessor, meaning it processes your Sass/SCSS files and outputs standard CSS files that web pages consume. This lets you use a more expressive syntax and advanced features while browsers still receive valid CSS.
The most common dialect today is SCSS (.scss), which looks almost like CSS with extra capabilities. The original indented Sass syntax (.sass) uses indentation instead of braces—less common in new projects.
$@include@extendSass offers numerous benefits that make it a preferred choice for many developers:
Sass files come in two syntaxes: the older indented syntax (.sass) and the newer SCSS syntax (.scss). SCSS is more commonly used because it is fully compatible with CSS—any valid CSS file is valid SCSS.
$primary-color: #333;
body {
font: 100% Helvetica, sans-serif;
color: $primary-color;
}
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}Compile with sass styles.scss styles.css after installing Dart Sass.
Variables in Sass start with a $ symbol and store values you reuse throughout your stylesheet—brand colors, font stacks, breakpoints, and spacing scales.
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}Nesting lets you nest CSS selectors in a way that follows the same visual hierarchy as your HTML. It reduces repetition but can produce overly specific selectors if overused.
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}Partials are Sass files meant to be imported into other files. Name them with a leading underscore (e.g. _variables.scss). Modern Sass prefers @use for namespacing; @import still appears in many tutorials and legacy codebases.
// _variables.scss
$primary-color: #333;
$secondary-color: #666;
// styles.scss
@use 'variables' as vars;
body {
color: vars.$primary-color;
}
h1 {
color: vars.$secondary-color;
}The older @import 'variables'; syntax works but is deprecated in favor of @use.
Mixins let you create reusable chunks of CSS. Pass arguments to make them flexible—ideal for vendor-prefix bundles, media-query breakpoints, or flex centering patterns.
@mixin border-radius($radius) {
border-radius: $radius;
}
.box {
@include border-radius(10px);
}Inheritance allows one selector to inherit styles from another using the @extend directive. Prefer mixins when you need parameterized styles; use @extend sparingly to avoid bloated selectors.
.message {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success { @extend .message; border-color: #3d9970; }
.error { @extend .message; border-color: #ff4136; }
.warning { @extend .message; border-color: #ff851b; }Sass supports math operators for calculations, making fluid layouts and grid math easier to read in source files.
.container {
width: 100%;
}
.content {
width: 600px / 960px * 100%;
}| Feature | SCSS syntax |
|---|---|
| Variable | $color: #333; |
| Nested rule | nav { a { ... } } |
| Mixin | @mixin name() { } @include name; |
| Extend | .child { @extend .parent; } |
| Partial import | @use 'variables' as v; |
| Compile | sass input.scss output.css |
Five compact SCSS snippets. Compile each with Dart Sass and link the resulting .css file in your HTML.
$brand: #2563eb;
$text: #1e293b;
body {
color: $text;
background: #f8fafc;
}
a { color: $brand; }.card {
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 8px;
h2 {
margin: 0 0 0.5rem;
font-size: 1.25rem;
}
p { margin: 0; color: #64748b; }
}@mixin flex-center {
display: flex;
align-items: center;
justify-content: center;
}
.hero {
min-height: 200px;
@include flex-center;
}%alert-base {
padding: 0.75rem 1rem;
border-radius: 6px;
}
.info {
@extend %alert-base;
background: #dbeafe;
}Placeholder selectors (%) extend without generating a standalone class in CSS output.
$columns: 12;
$span: 4;
.col {
width: ($span / $columns) * 100%;
}Author .scss files with variables, nesting, and partials.
Dart Sass resolves imports, expands mixins, and evaluates expressions.
Plain .css is generated—no Sass syntax remains.
Link the CSS in HTML; the browser uses standard CSS rules only.
Sass is a robust tool that enhances CSS by adding features that help you write cleaner, more efficient, and more manageable stylesheets. By incorporating Sass into your workflow, you can streamline CSS development and maintain a more organized codebase.
@use and shallow nesting in modern projects._variables.scss, _mixins.scss, _components.scss@use over deprecated @import$color-text-muted not $gray2@extend across unrelated componentsSass was originally created by Hampton Catlin and Natalie Weizenbaum in 2006. The name reflects its goal: to make stylesheets syntactically awesome. Today Dart Sass is the official implementation, and SCSS is the syntax most developers write day to day.
Install Dart Sass, write a small SCSS file with variables, and compile it to CSS.
8 people found this page helpful