Snelle referentie

Sass (SCSS) spiekbriefje

Uitgebreide referentie voor Sass en SCSS: variabelen, nesting, bovenliggende selector &, mixins, functies en het @use/@forward modulesysteem.

· Gepubliceerd

Sass is een volwassen, stabiele en krachtige CSS-preprocessor die CSS uitbreidt met variabelen, nesting, mixins en modules.

Variabelen en nesting (Variables & Nesting)

$primary-color: #2563eb;
$border-radius: 8px;

.card {
  background: #ffffff;
  border-radius: $border-radius;

  .title {
    color: $primary-color;
  }

  &:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  &__header {
    padding: 16px;
  }
}

Mixins en functies (Mixins & Functions)

// Define reusable mixin with default arguments
@mixin flex-center($direction: row) {
  display: flex;
  flex-direction: $direction;
  align-items: center;
  justify-content: center;
}

.hero {
  @include flex-center(column);
  height: 400px;
}
// Functions & Calculations
@use 'sass:math';

@function rem($pixels) {
  @return math.div($pixels, 16px) * 1rem;
}

.heading {
  font-size: rem(32px); // => 2rem
}

Modern modulesysteem (@use en @forward)

// _variables.scss
$theme-accent: #10b981;

// main.scss
@use 'variables' as vars;

.button {
  background-color: vars.$theme-accent;
}

Gerelateerde spiekbriefjes

Verwante onderwerpen