ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Sass Media Query Mixin for Responsive Design

SASS

Goal -- WPM

Ready
Exercise Algorithm Area
1// Mixin for creating responsive media queries.
2// Simplifies the process of applying styles at different screen sizes.
3
4@mixin respond-to($breakpoint) {
5// Validate breakpoint input.
6@if type-of($breakpoint) != map {
7@warn "Invalid breakpoint type. Expected a map for breakpoints.";
8@return;
9}
10
11// Extract the first key-value pair from the map.
12$name: map-get($breakpoint, "name");
13$value: map-get($breakpoint, "value");
14
15@if not $name or not $value {
16@warn "Breakpoint map must contain 'name' and 'value' keys.";
17@return;
18}
19
20// Construct the media query string.
21$media-query: "";
22@if $name == "max-width" {
23$media-query: "(max-width: #{$value})";
24} @else if $name == "min-width" {
25$media-query: "(min-width: #{$value})";
26} @else {
27@warn "Unsupported breakpoint name: '#{$name}'. Use 'max-width' or 'min-width'.";
28@return;
29}
30
31// Apply styles within the media query.
32@media #{$media-query} {
33@content;
34}
35}
36
37// Define common breakpoints.
38$breakpoints: (
39"small": "576px",
40"medium": "768px",
41"large": "992px",
42"xlarge": "1200px"
43);
44
45// Example usage:
46.container {
47width: 100%;
48
49// Apply styles for small screens and up.
50@include respond-to(("name": "min-width", "value": map-get($breakpoints, "small"))) {
51width: 90%;
52}
53
54// Apply styles for medium screens and up.
55@include respond-to(("name": "min-width", "value": map-get($breakpoints, "medium"))) {
56width: 80%;
57}
58
59// Apply styles for large screens and up.
60@include respond-to(("name": "min-width", "value": map-get($breakpoints, "large"))) {
61width: 75%;
62}
63
64// Example of max-width query.
65@include respond-to(("name": "max-width", "value": "600px")) {
66background-color: lightblue;
67}
68}
69
70// Invalid usage example.
71.invalid-element {
72@include respond-to("not a map");
73}
Algorithm description viewbox

Sass Media Query Mixin for Responsive Design

Algorithm description:

This Sass mixin, `respond-to`, simplifies the creation of media queries for responsive web design. It accepts a map containing a breakpoint name (like 'min-width' or 'max-width') and its corresponding value (e.g., '768px'). The mixin then generates the appropriate `@media` rule, allowing developers to easily apply styles that adapt to different screen sizes. This promotes maintainable and organized responsive code.

Algorithm explanation:

The `respond-to` mixin takes a Sass map as input, which must contain 'name' and 'value' keys. It validates that the input is a map and that these keys exist. It then constructs the media query string based on the 'name' (e.g., 'min-width', 'max-width') and 'value'. If an unsupported name is provided, a warning is issued. The `@content` directive allows the user to pass in styles that will be wrapped by the generated media query. The time complexity is effectively O(1) as it performs a fixed set of operations regardless of the number of breakpoints used. Space complexity is also O(1) as it doesn't store significant data structures.

Pseudocode:

DEFINE MIXIN respond-to(breakpoint_map)
  IF breakpoint_map is NOT a map THEN
    WARN "Invalid breakpoint type"
    RETURN
  END IF

  name = get value for key "name" from breakpoint_map
  value = get value for key "value" from breakpoint_map

  IF name is missing OR value is missing THEN
    WARN "Breakpoint map must contain 'name' and 'value'"
    RETURN
  END IF

  media_query_string = ""
  IF name is "max-width" THEN
    media_query_string = "(max-width: " + value + ")"
  ELSE IF name is "min-width" THEN
    media_query_string = "(min-width: " + value + ")"
  ELSE
    WARN "Unsupported breakpoint name"
    RETURN
  END IF

  APPLY styles using @media media_query_string
    PASS THROUGH @content
  END APPLY
END MIXIN