Getting Started
4 snippetsSCSS basics and syntax
Comments
// Single line comment
/* Multi-line
comment */Import
@import 'variables';
@import 'mixins';Nesting
.nav {
background: blue;
a {
color: white;
&:hover {
color: yellow;
}
}
}Parent Selector (&)
.button {
&:hover { }
&.active { }
&--large { } // BEM
}Variables
5 snippetsStoring and reusing values
Define Variables
$primary-color: #3498db;
$font-stack: Helvetica, sans-serif;
$base-spacing: 1rem;Use Variables
.header {
color: $primary-color;
font-family: $font-stack;
padding: $base-spacing;
}Variable Scope
$global: red;
.block {
$local: blue; // Only in .block
color: $local;
}Default Values
$color: blue !default;
// Won't override if already setInterpolation
$name: "primary";
.alert-#{$name} {
color: red;
}Mixins
4 snippetsReusable style blocks with parameters
Define Mixin
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}Use Mixin
.button {
@include border-radius(5px);
}Default Parameters
@mixin box-shadow($x: 0, $y: 2px, $blur: 4px) {
box-shadow: $x $y $blur rgba(0,0,0,0.3);
}Content Block
@mixin respond-to($breakpoint) {
@media (min-width: $breakpoint) {
@content;
}
}
.container {
@include respond-to(768px) {
width: 750px;
}
}Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Functions
4 snippetsBuilt-in and custom functions
Color Functions
darken($color, 10%);
lighten($color, 10%);
saturate($color, 20%);
desaturate($color, 20%);
alpha($color);Math Functions
percentage(0.5); // 50%
round(10.6); // 11
ceil(10.1); // 11
floor(10.9); // 10
abs(-10); // 10Custom Function
@function calculate-rem($px) {
@return $px / 16px * 1rem;
}
.text {
font-size: calculate-rem(18px);
}String Functions
quote("hello"); // "hello"
unquote("hello"); // hello
str-length("hello"); // 5
to-upper-case("hi"); // HIExtend & Placeholder
2 snippetsSharing styles between selectors
Extend
.error {
border: 1px red;
background: #fdd;
}
.serious-error {
@extend .error;
border-width: 3px;
}Placeholder Selector
%button-base {
padding: 10px;
border: none;
}
.btn-primary {
@extend %button-base;
background: blue;
}Operators
3 snippetsMath and logic operations
Math Operators
width: 100% / 3;
height: $base-height + 20px;
margin: $spacing * 2;
padding: $value - 10px;Comparison
@if $width > 1000px {
width: 100%;
} @else {
width: 90%;
}Boolean
@if $theme == "dark" and $high-contrast {
color: white;
}Control Flow
4 snippetsIf, for, each, and while
If/Else
@if $theme == "dark" {
background: black;
} @else if $theme == "light" {
background: white;
} @else {
background: gray;
}For Loop
@for $i from 1 through 3 {
.col-#{$i} {
width: 100% / $i;
}
}Each Loop
@each $color in red, green, blue {
.#{$color}-text {
color: $color;
}
}Each with Map
$sizes: (small: 12px, medium: 16px, large: 20px);
@each $name, $size in $sizes {
.text-#{$name} {
font-size: $size;
}
}Maps & Lists
4 snippetsData structures in SCSS
Define Map
$colors: (
primary: #3498db,
secondary: #2ecc71,
danger: #e74c3c
);Access Map
color: map-get($colors, primary);
// Or with module system
color: map.get($colors, primary);Lists
$fonts: Helvetica, Arial, sans-serif;
$sizes: 10px 20px 30px;List Functions
length($fonts); // 3
nth($fonts, 1); // Helvetica
append($fonts, Monaco);