The Sass built-in list module: an introduction with code examples

Sass is a CSS preprocessor that extends the functionalities of CSS with advanced features such as variables, mixins, functions, and more. One of the notable features in Sass is its built-in list module, which offers a suite of tools to handle lists efficiently.

A list in Sass is an ordered collection of values separated by commas. Lists can contain any type of value, including numbers, strings, colors, and even other lists. Lists can be used to store and manipulate data in Sass.

Example lists

Here's an example of a list in Sass:

$breakpoints: (
  320px,
  480px,
  720px,
  1024px
);

The built-in list module provides functions for working with lists, including creating, manipulating, and transforming lists. Some of the most useful functions include:

  • nth: Returns the nth item in a list.

Here's an example of how to use the nth function:

$first-breakpoint: nth($breakpoints, 1);
// $first-breakpoint: 320px
  • length: Returns the number of items in a list.

Here's an example of how to use the length function:

$num-breakpoints: length($breakpoints);
// $num-breakpoints: 4
  • append: Adds an item to the end of a list.

Here's an example of how to use the append function:

$new-breakpoints: append($breakpoints, 1280px);
// $new-breakpoints: (320px, 480px, 720px, 1024px, 1280px)
  • join: Combines two lists into a single list.

Here's an example of how to use the join function:

$media-queries: (
  "(min-width: 320px)",
  "(min-width: 480px)",
  "(min-width: 720px)",
  "(min-width: 1024px)"
);

$breakpoint-queries: join($breakpoints, $media-queries, " ");
// $breakpoint-queries: (320px "min-width: 320px", 480px "min-width: 480px", 720px "min-width: 720px", 1024px "min-width: 1024px")

  • map-get: Returns the value of an item in a map, which is a type of list with keys and values.

Here's an example of how to use the map-get function:

$colors: (
  "primary": #0074d9,
  "secondary": #7fdbff,
  "tertiary": #39cccc
);

$primary-color: map-get($colors, "primary");
// $primary-color: #0074d9

Lists in Sass can be useful for a variety of tasks, such as creating responsive designs, generating content, and simplifying complex CSS. For example, you can use a list of breakpoints to create a responsive design that changes based on the size of the screen:

@each $breakpoint in $breakpoints {
  @media (min-width: $breakpoint) {
    .container {
      max-width: $breakpoint;
    }
  }
}

In this example, the @each loop iterates over each breakpoint in the $breakpoints list, and for each one, creates a media query that sets the max-width of the .container class to that breakpoint.

Lists can also be used to store data, such as color values or font names, which can be used throughout your stylesheet. For example, you can use a list of colors to keep your color values organized and consistent:

$colors: (
  "primary": #0074d9,
  "secondary": #7fdbff,
  "tertiary": #39cccc
);

body {
  background-color: map-get($colors, "primary");
  color: map-get($colors, "secondary");
}

In this example, the $colors list is used to store the color values, and the map-get function is used to retrieve the values from the list and apply them to the body element.

Conclusion

In conclusion, the SASS built-in list module provides a powerful tool for working with lists in Sass. By using the functions provided, you can simplify and streamline your stylesheets, making them easier to maintain and manage. Whether you are a seasoned Sass user or just getting started, the built-in list module is an essential tool for any Sass developer.


Additional resources
  • Frontend web development courses

    Beginner-friendly courses focusing on HTML, CSS, and JavaScript.

    View Courses
  • Frontend web development projects

    Beginner-friendly projects focusing on HTML, CSS, and JavaScript.

    View Projects
  • Free website templates

    Collection of free, high-quality website templates for your next project.

    View Templates