【问题标题】:Concatenate Sass variable within loop在循环内连接 Sass 变量
【发布时间】:2020-05-22 08:13:04
【问题描述】:

我虽然可以像这样连接:

$sizes: 6;

@for $i from 1 through $sizes {
    .font-size-#{$i} {
        font-size: $h#{($i)}-font-size;
    }
}

当我有这些变量时:

$h1-font-size: 3.5rem;
$h2-font-size: 3rem;
$h3-font-size: 2.5rem;
$h4-font-size: 2rem;
$h5-font-size: 1.5rem;
$h6-font-size: 1rem;

基本上我想创建字体大小类以匹配标题变量,例如:

.font-size-1 {
   font-size: 3.5rem;
}
.font-size-2 {
   font-size: 3rem;
}

等等……

有什么想法吗?

【问题讨论】:

标签: sass compass-sass


【解决方案1】:

您是否必须将字体大小存储在单独的变量中,还是可以将它们存储在地图中?

如果地图是一个选项,你可以这样解决它:

$heading-sizes: (
  1: 3.5rem,
  2: 3rem,
  3: 2.5rem,
  4: 2rem,
  5: 1.5rem,
  6: 1rem
);

@each $heading, $size in $heading-sizes {
  .font-size-#{$heading} {
    font-size: $size;
  }
}

【讨论】:

  • 我将所有内容都存储在变量中,但我喜欢这个解决方案。你认为它甚至可以使用变量吗?
猜你喜欢
  • 1970-01-01
  • 2013-02-13
  • 2015-04-30
  • 2017-02-24
  • 2017-07-11
  • 1970-01-01
  • 2018-02-27
  • 2012-10-05
  • 2018-11-17
相关资源
最近更新 更多