简体   繁体   中英

Grouping Sass Selectors When Using @for

I have a group of heading levels (1 through 6). I am using a @for loop but I can't seem to work out how to group them as one instead of individually.

This is the @for loop I'm using:

@for $i from 1 through 6 {
   h#{$i},
   .h#{$i} {
      margin-bottom: $headings-margin-bottom;
      line-height: $headings-line-height;
      font-weight: $headings-font-weight;
      font-family: $headings-font-family;
   }
}

Here's what I am expecting:

h1,
h2,
h3,
h4,
h5,
h6 {
   /* styles */
}

Here's what is getting compiled:

h1 {
   /* styles */
}

h2 {
   /* styles */
}

h3 {
   /* styles */
}

...

You can use @extend :

%myStyle {
  margin-bottom: $headings-margin-bottom;
  line-height: $headings-line-height;
  font-weight: $headings-font-weight;
  font-family: $headings-font-family;
}

@for $i from 1 through 6 {
   h#{$i},
   .h#{$i} {
      @extend %myStyle;
   }
}

The @extend will let you join the selectors. The symbol % is a placeholder selector , you can use it so myStyle will not show up in the compiled CSS .

More info here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM