When working on documentation for my product, LiveAPI, I started using MkDocs, a static site generator that produces clean and professional documentation.
However, I found its design a bit monotonous and decided to explore the project to make some customizations.
This journey led me to an intriguing part of its architecture: Sass maps.
What started as a casual modification turned into a deeper appreciation for the thoughtful design behind this project.
In this blog, I’ll explore how Sass maps are used in MkDocs’ Material theme—specifically, in its mixins—and how they contribute to the flexibility and scalability of the design system. Let’s dive in!
Sass maps are a key-value data structure in Sass, akin to dictionaries in Python or objects in JavaScript.
They allow us to store related data compactly and retrieve values dynamically.
In the MkDocs Material theme, Sass maps are used to define device-specific breakpoints for responsive design. For example:
@use "sass:map"; @use "sass:list"; @use "sass:math"; $break-devices: ( mobile: ( portrait: 220px 479px, landscape: 480px 719px, ), tablet: ( portrait: 720px 959px, landscape: 960px 1219px, ), screen: ( small: 1220px 1599px, large: 1600px 1999px, ), ) !default;
This map organizes breakpoints into categories (mobile, tablet, screen) and subcategories (portrait, landscape, small, medium, large).
It’s not just a static definition—it’s the backbone of how responsive behaviors are generated dynamically.
The theme uses a series of functions and mixins to extract and utilize data from the $break-devices map. Here’s a breakdown:
The break-select-device function traverses the map to find the relevant device category and retrieves the associated breakpoints.
If multiple levels are specified (e.g., mobile portrait), it digs deeper into the hierarchy.
@function break-select-device($device) { $current: $break-devices; @for $n from 1 through length($device) { @if type-of($current) == map { $current: map.get($current, list.nth($device, $n)); } @else { @error "Invalid device map: #{$devices}"; } } @return $current; }
In SCSS, a mixin is a reusable block of code that you can define once and use throughout your stylesheet.
It helps keep your code DRY (Don't Repeat Yourself) by allowing you to include styles or logic multiple times without repeating the code.
For example, if you need to apply a set of styles repeatedly, you can create a mixin and reuse it wherever required:
The break-from-device and break-to-device mixins leverage this function to dynamically generate media queries. For instance:
@use "sass:map"; @use "sass:list"; @use "sass:math"; $break-devices: ( mobile: ( portrait: 220px 479px, landscape: 480px 719px, ), tablet: ( portrait: 720px 959px, landscape: 960px 1219px, ), screen: ( small: 1220px 1599px, large: 1600px 1999px, ), ) !default;
This mixin applies styles for a minimum width specified in the map. A similar approach is used for the break-to-device mixin, which targets a maximum width.
To see the power of the break-from-device and break-to-device mixins, let’s look at practical examples of how to use them to implement responsive styles dynamically.
By default, we apply styles for mobile screens using a grid layout that works well on small screens without the need for a mixin. For example:
@function break-select-device($device) { $current: $break-devices; @for $n from 1 through length($device) { @if type-of($current) == map { $current: map.get($current, list.nth($device, $n)); } @else { @error "Invalid device map: #{$devices}"; } } @return $current; }
In this case, the layout is already optimized for mobile devices. The base styles cater to mobile users by default.
To enhance responsiveness for larger screens, you can use the break-from-device mixin to target specific breakpoints.
On smaller screens, such as tablets up to the landscape breakpoint, certain elements like the sidebar may not fit well due to limited screen space.
In such cases, we can hide the sidebar and show it as a popover from left to prioritize the main content. For example:
@mixin break-from-device($device) { @if type-of($device) == string { $device: $device; } @if type-of($device) == list { $breakpoint: break-select-device($device); $min: list.nth($breakpoint, 1); @media screen and (min-width: $min) { @content; } } @else { @error "Invalid device: #{$device}"; } } @mixin break-to-device($device) { @if type-of($device) == string { $device: $device; } @if type-of($device) == list { $breakpoint: break-select-device($device); $max: list.nth($breakpoint, 2); @media screen and (max-width: $max) { @content; } } @else { @error "Invalid device: #{$device}"; } }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(1, 1fr); /* 1 column for small screens */ }
For devices larger than the tablet landscape breakpoint, where more screen space is available, we can introduce a two-column layout for improved content distribution. This can be achieved using the break-from-device mixin:
@include break-to-device(tablet landscape) { .sidebar { display: none; } }
@media screen and (max-width: 1219px) { .sidebar { display: none; } }
As screen sizes increase, more space becomes available to present content.
For desktops, we can adjust the grid layout to create three or four columns, depending on the screen size, using the break-from-device mixin.
When the screen size is large enough to accommodate three columns, the following styles apply:
@use "sass:map"; @use "sass:list"; @use "sass:math"; $break-devices: ( mobile: ( portrait: 220px 479px, landscape: 480px 719px, ), tablet: ( portrait: 720px 959px, landscape: 960px 1219px, ), screen: ( small: 1220px 1599px, large: 1600px 1999px, ), ) !default;
@function break-select-device($device) { $current: $break-devices; @for $n from 1 through length($device) { @if type-of($current) == map { $current: map.get($current, list.nth($device, $n)); } @else { @error "Invalid device map: #{$devices}"; } } @return $current; }
For even larger screens, we can create four columns to maximize the use of screen real estate:
@mixin break-from-device($device) { @if type-of($device) == string { $device: $device; } @if type-of($device) == list { $breakpoint: break-select-device($device); $min: list.nth($breakpoint, 1); @media screen and (min-width: $min) { @content; } } @else { @error "Invalid device: #{$device}"; } } @mixin break-to-device($device) { @if type-of($device) == string { $device: $device; } @if type-of($device) == list { $breakpoint: break-select-device($device); $max: list.nth($breakpoint, 2); @media screen and (max-width: $max) { @content; } } @else { @error "Invalid device: #{$device}"; } }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(1, 1fr); /* 1 column for small screens */ }
This design shows the author’s intent to prioritize scalability and maintainability.
By abstracting breakpoints into a single source of truth and using mixins for media queries, they’ve created a system that:
Exploring MkDocs Material has deepened my appreciation for thoughtful design in front-end architecture.
The use of Sass maps, mixins, and hierarchical data structures is a masterclass in maintainable and scalable design systems.
If you’re looking to create or improve your own responsive styles, consider adopting similar techniques.
Have you encountered or used Sass maps in your projects? I’d love to hear your experiences and insights!
Want to dive deeper into the designing world? Check out our other blog posts:
Subscribe for a weekly dose of insights on development, IT, operations, design, leadership and more.
The above is the detailed content of How This SCSS Project Stays Organized Starting from a Map. For more information, please follow other related articles on the PHP Chinese website!