0Pricing
CSS Academy · Lesson

Mixins and Includes

Create reusable style blocks with @mixin and include them anywhere with @include.

Mixins and Includes is a free CSS Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a Mixin?

A mixin in Sass is a reusable block of CSS declarations. Unlike a regular class, it can accept parameters, making it more flexible than copy-paste. Define with @mixin, use with @include.

Basic Mixin

A mixin without parameters is just a reusable block:

@mixin flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.hero {
  @include flex-center;
  min-height: 100dvh;
}

Mixin with Arguments

Pass values to a mixin to customize its output:

@mixin button-variant($bg, $color: white) {
  background-color: $bg;
  color: $color;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;

  &:hover {
    background-color: darken($bg, 10%);
  }
}

.btn-primary { @include button-variant(royalblue); }
.btn-danger  { @include button-variant(crimson); }

Default Arguments

Set default values for mixin arguments so they are optional:

@mixin shadow($x: 0, $y: 4px, $blur: 16px, $color: rgba(0,0,0,0.1)) {
  box-shadow: $x $y $blur $color;
}

.card  { @include shadow(); }
.modal { @include shadow(0, 8px, 32px, rgba(0,0,0,0.25)); }

Keyword Arguments

Pass mixin arguments by name for clarity:

.tooltip { @include shadow($blur: 8px, $color: rgba(0,0,0,0.2)); }

@content — Mixin with Block

Mixins can accept a block of content using @content. The included block is injected where @content appears:

@mixin hover {
  &:hover, &:focus {
    @content;
  }
}

.link {
  @include hover {
    color: royalblue;
    text-decoration: underline;
  }
}

Responsive Mixin

A common mixin for media queries:

@mixin respond-to($breakpoint) {
  @if $breakpoint == "tablet" {
    @media (min-width: 768px) { @content; }
  } @else if $breakpoint == "desktop" {
    @media (min-width: 1024px) { @content; }
  }
}

.sidebar {
  width: 100%;

  @include respond-to("desktop") {
    width: 280px;
  }
}

Typography Mixin

A text style mixin for consistent type scale:

@mixin text-style($size, $weight: 400, $line-height: 1.5) {
  font-size: $size;
  font-weight: $weight;
  line-height: $line-height;
}

.heading { @include text-style(2rem, 700, 1.2); }
.body    { @include text-style(1rem, 400, 1.6); }

Mixin vs Function

Mixins output CSS declarations; functions return a value. Use a mixin when you want to output multiple CSS rules; use a function when you need a computed single value.

Avoiding Mixin Overuse

Too many mixins create abstract indirection that is hard to debug. Only create a mixin if a pattern is reused 3+ times. For one-off styles, just write the CSS directly.

Quick Check

What is the purpose of @content inside a Sass mixin definition?

Recap

Define mixins with @mixin and use them with @include. Mixins accept parameters with optional defaults. @content allows callers to inject CSS blocks into the mixin — enabling wrapper patterns like media query helpers. Create mixins only for patterns reused multiple times to avoid abstraction overhead.

Frequently asked questions

Is the “Mixins and Includes” lesson free?

Yes — the full text of “Mixins and Includes” is free to read here on the web, and the CSS Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO.

What will I learn in “Mixins and Includes”?

Create reusable style blocks with @mixin and include them anywhere with @include. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CSS Academy?

No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Mixins and Includes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CSS Academy lesson?

Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Variables Nesting and Partials
  2. Mixins and Includes
  3. Extend and Placeholder Selectors
  4. Sass Functions and Control Flow
← Back to CSS Academy