And*_*dai 6 css sass css-variables
我正在尝试在 CSS/scss 中构建我自己的小型可扩展网格。到目前为止,我发现了这个决定:
:root {
--page-width: 1170px;
--gutter: 15px;
--columns: 12;
}
.wrapper {
max-width: var(--page-width);
margin-right: auto;
margin-left: auto;
padding-left: var(--gutter);
padding-right: var(--gutter);
}
.row {
margin-left: calc(-1 * var(--gutter));
margin-right: calc(-1 * var(--gutter));
}
.col {
display: block;
margin-left: var(--gutter);
margin-right: var(--gutter);
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用 scss 来缩短列类描述(同时允许我在整个代码的一个地方更改列数 - 在 CSS Variable 中--columns)像这样
@for $n from 1 through var(--columns) {
.col-#{$n} {width: calc( #{$n} / var(--columns) - var(--gutter) * 2 ); }
}
Run Code Online (Sandbox Code Playgroud)
但它没有用。有趣的细节是,当我将@for语句从@for $n from 1 throughvar(--columns)``更改为它时,@for $n from 1 through12它编译得很好。并且在@forbody内部编译 CSS-Variable 没有问题。.col-#{$n} {width: calc( #{$n} / var(--columns) - var(--gutter) * 2 ); }很好地编译成需要的一系列类。
如果我使用 scss 变量$columns而不是 CSS 变量,那么我需要将我的 grid.scss 文件导入到项目的所有其他 scss 文件中。
这是我关于 StackOverflow 的第一个问题,所以如果需要任何其他详细信息,请告诉我。
Jak*_*b E 16
CSS 和 SCSS 变量是两个截然不同的东西(请看这支笔)
为了让它工作,你需要一个静态变量来供 SCSS 编译
// static (SCSS) variables used produce CSS output
$page-width: 1170px;
$gutter : 15px
$columns: 12;
// dynamic (CSS) variables used at run-time
// note the values are interpolated
:root {
--page-width: #{$page-width};
--gutter : #{$gutter};
--columns: #{$columns};
}
// the for loop is aimed at producing CSS output
// ... why you need the static variable
@for $n from 1 through $columns {
// the content becomes CSS output
// ... why you can use dynamic variables
.col-#{$n} {width: calc( #{$n} / var(--columns) - var(--gutter) * 2 ); }
}
Run Code Online (Sandbox Code Playgroud)
接受的答案不再有效。较新版本的 SASS 要求对变量使用插值。
请参阅此处了解更多详情
$accent-color: #fbbc04;
:root {
// WRONG, will not work in recent Sass versions.
--accent-color-wrong: $accent-color;
// RIGHT, will work in all Sass versions.
--accent-color-right: #{$accent-color};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4966 次 |
| 最近记录: |