Sass Mixins
Mixins are one of the most powerful features in Sass, allowing you to create reusable chunks of CSS code. Natours uses three essential mixins that solve common layout and responsive design challenges.Overview
All mixins are defined insass/abstracts/_mixins.scss and can be imported using Sass’s @use directive:
Mixins are compiled into standard CSS at build time, so there’s no runtime performance cost.
Clearfix Mixin
Theclearfix mixin solves the classic “float collapse” problem where parent containers don’t properly contain their floated children.
Source Code
sass/abstracts/_mixins.scss
How It Works
Real-World Usage
The clearfix mixin is used in the grid system to ensure rows properly contain their floated columns:sass/components/layout/_grid.scss
When to Use
- Container elements with floated children
- Legacy grid systems using float-based layouts
- Situations where flexbox or grid aren’t appropriate
Absolute Center Mixin
TheabsCenter mixin perfectly centers an element both horizontally and vertically within its positioned parent.
Source Code
sass/abstracts/_mixins.scss
How It Works
Move to center point
top: 50% and left: 50% position the element’s top-left corner at the parent’s centerReal-World Usage
The mixin is used extensively throughout the project for centering content:When to Use
- Modal dialogs and popups
- Overlay content on images or videos
- Call-to-action sections within cards
- Icon centering within circular containers
The parent element must have
position: relative, position: absolute, or position: fixed for this to work.Visual Example
In the card component, the call-to-action section is perfectly centered on the back side of the flipping card:Respond Mixin
Therespond mixin is Natours’ responsive design solution, providing a consistent interface for media queries across the entire project.
Source Code
sass/abstracts/_mixins.scss
Breakpoint System
phone
0 - 600px
max-width: 37.5emMobile devices in portrait modetab-port
600px - 900px
max-width: 56.25emTablets in portrait orientationtab-land
900px - 1200px
max-width: 75emTablets in landscape orientationbig-desktop
1800px+
min-width: 112.5emLarge desktop monitorsThe default styles (1200px - 1800px) don’t need a media query - they’re the base case.
Why Em Units?
Media queries useem units instead of px for better browser compatibility:
1em = 16px(browser default)37.5em × 16 = 600px56.25em × 16 = 900px75em × 16 = 1200px112.5em × 16 = 1800px
Real-World Usage
The respond mixin implements a mobile-first strategy by adjusting the base font size:sass/base/_base.scss
How Font Scaling Works
Usage Pattern
When to Use Each Breakpoint
| Breakpoint | Use Case | Example |
|---|---|---|
phone | Stack columns, hide decorative elements | Navigation becomes hamburger menu |
tab-port | Reduce spacing, adjust grid columns | 3-column grid becomes 2-column |
tab-land | Minor spacing adjustments | Slightly reduce padding |
big-desktop | Increase font sizes, add spacing | Make content more readable on large monitors |
Best Practices
Keep mixins simple
Each mixin should solve one specific problem
Use semantic names
absCenter is clearer than centerHelperDocument parameters
Include comments explaining what arguments do
Import once
Use
@use instead of @import to avoid duplicationRelated Topics
Media Queries
Deep dive into the responsive design system
Variables
Learn about Sass variables and the color system