Sass @each variable interpolation [duplicate]

2019-01-06 22:48发布

This question already has an answer here:

In my newest site I am trying to make a massive effort to use Sass features to make my life easier.

I am writing some simple error box styles and thought that using each would simplify them a bit.

I have the following:

$message-box-types: error success normal

@each $type in $message-box-types
  %#{$type}-box
    @extend %message-box
    border-color: $message-#{$type}
    color: $message-#{$type}
    background-color: lighten($message-#{$type}, 20%)

The errors identify both lines with $message-#{$type}

I had a similar issue with a previous @each statement I was trying to write but ended up ignoring it and writing it out in scratch as I thought it was something Sass couldn't handle.

Anyone have any ideas?

Neil

标签: css sass each
1条回答
该账号已被封号
2楼-- · 2019-01-06 23:22

With Maptastic Maple (v3.3), you can use some simple map functions here

$message-box-types:
  foo #ccc,
  bar #ffffd

@each $type, $color in $message-box-types
  %#{$type}-box
    @extend %message-box
    border-color: $color
    color: $color
    background-color: lighten($color, 20%)
查看更多
登录 后发表回答