Home > Web Front-end > CSS Tutorial > How to Dynamically Generate Comma-Separated Column Classes in SCSS?

How to Dynamically Generate Comma-Separated Column Classes in SCSS?

Susan Sarandon
Release: 2024-12-07 22:01:16
Original
340 people have browsed it

How to Dynamically Generate Comma-Separated Column Classes in SCSS?

Dynamically Generating List of Column Classes with Comma Separation

It's a common scenario in grid systems to generate a dynamic list of column classes based on a given number of columns. SCSS, an extension of CSS, provides a powerful solution for such scenarios. However, it can be challenging to achieve the desired comma-separated list of classes.

To create a dynamic list of column classes with commas separating them, you can leverage the powerful @extend directive in SCSS. Here's how it works:

  1. Define a placeholder class with the desired styles:
%float-styles {
  float: left;
}
Copy after login
  1. Create a mixin that uses @extend to apply those styles to the generated column classes:
@mixin col-x-list {
  @for $i from 1 through $columns {
      .col-#{$i}-m { @extend %float-styles; }
  }
}
Copy after login
  1. Call the mixin to generate the column classes:
@include col-x-list;
Copy after login
  1. Define $columns with the desired number of columns:
$columns: 12;
Copy after login

This approach will generate a comma-separated list of column classes with the desired float style, such as:

.col-1-m, .col-2-m, .col-3-m, .col-4-m, .col-5-m, .col-6-m, .col-7-m, .col-8-m, .col-9-m, .col-10-m, .col-11-m, .col-12-m {
  float: left;
}
Copy after login

By using @extend, you can effectively apply styles from one selector to another, helping you achieve a dynamically generated list of column classes with minimal code duplication.

The above is the detailed content of How to Dynamically Generate Comma-Separated Column Classes in SCSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template