How This SCSS Project Stays Organized Starting from a Map
Jan 01, 2025 pm 01:54 PMWhen 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!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

CSSismostlycase-insensitive,butURLsandfontfamilynamesarecase-sensitive.1)Propertiesandvalueslikecolor:red;arenotcase-sensitive.2)URLsmustmatchtheserver'scase,e.g.,/images/Logo.png.3)Fontfamilynameslike'OpenSans'mustbeexact.

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin

TocreatestickyheadersandfooterswithCSS,useposition:stickyforheaderswithtopvalueandz-index,ensuringparentcontainersdon’trestrictit.1.Forstickyheaders:setposition:sticky,top:0,z-index,andbackgroundcolor.2.Forstickyfooters,betteruseposition:fixedwithbot

The scope of CSS custom properties depends on the context of their declaration, global variables are usually defined in :root, while local variables are defined within a specific selector for componentization and isolation of styles. For example, variables defined in the .card class are only available for elements that match the class and their children. Best practices include: 1. Use: root to define global variables such as topic color; 2. Define local variables inside the component to implement encapsulation; 3. Avoid repeatedly declaring the same variable; 4. Pay attention to the coverage problems that may be caused by selector specificity. Additionally, CSS variables are case sensitive and should be defined before use to avoid errors. If the variable is undefined or the reference fails, the fallback value or default value initial will be used. Debug can be done through the browser developer

CSSanimationsenhancewebpagesbyimprovinguserexperienceandsitefunctionality.1)Usetransitionsforsmoothstylechanges,asinthebuttoncolorexample.2)Employkeyframesfordetailedanimations,likethebouncingball.3)Ensureperformancebykeepinganimationssimpleandusingt
