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!
What Are Sass Maps?
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.
How Sass Maps Work in MkDocs Mixins
The theme uses a series of functions and mixins to extract and utilize data from the $break-devices map. Here’s a breakdown:
1. Extracting Breakpoint Values
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; }
- @for Loop: This loop runs from the first to the last item in the $device list, ensuring every level in the hierarchy is checked.
- @if Condition: This checks if the current value is still a map and continues digging if true. If not, it halts with an error message.
- map.get: A built-in Sass function that retrieves a value from the map using a key.
2. Creating Media Queries
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.
Applying the Mixins
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.
Example 1: Applying Default Styles for Mobile Devices
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.
Example 2: Targeting Tablet Landscape
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}"; } }
- tablet landscape: Refers to the tablet category and its landscape subcategory in the $break-devices map.
- The generated media query will be:
.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; } }
- The generated media query will be:
@media screen and (max-width: 1219px) { .sidebar { display: none; } }
Example 3: Targeting Desktops
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.
For small desktops:
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;
- desktop small: Refers to the desktop category and its small subcategory in the $break-devices map.
- The generated media query will be:
@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 large desktops:
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}"; } }
- desktop large: Refers to the desktop category and its large subcategory in the $break-devices map.
- The generated media query will be:
.grid { display: grid; gap: 16px; grid-template-columns: repeat(1, 1fr); /* 1 column for small screens */ }
Architectural Elegance
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:
- Is Easy to Maintain: Updating breakpoints or adding new ones doesn’t require hunting through the codebase.
- Enhances Readability: Media queries are abstracted into logical, reusable components.
- Promotes Scalability: New devices or categories can be added to the map without breaking existing functionality.
Final Thoughts
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:
- Basic Rules of Design for Non-Designers
- Powerful UI Design and Implementation Principles
- Making Great Widgets
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!

What it looks like to troubleshoot one of those impossible issues that turns out to be something totally else you never thought of.

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
