使用SASS/SCSS生成CSS变量
我目前正在使用 SCSS 尝试重新创建 CSS 块。 CSS 块如下所示:
:root {
--Franklin-Blue: #1d1c4d;
--Light-Blue: #4e5d94;
--Pale-Blue: #7289da;
--Pure-White: #ffffff;
--VLight-Grey: #99aab5;
--Middle-Grey: #2c2f33;
--Dark-Grey: #23272a;
--body-color: #ffffff;
--text-color: #000000;
--bold-text: #000000;
--White-Blue: #ffffff;
}
[data-theme="dark"] {
--Franklin-Blue: #000000;
--Light-Blue: #0d0d0d;
--Pale-Blue: #2c2f33;
--Pure-White: #ffffff;
--VLight-Grey: #99aab5;
--Middle-Grey: #2c2f33;
--Dark-Grey: #23272a;
--body-color: #000000;
--text-color: #ffffff;
--bold-text: #7289da;
--White-Blue: #7289da;
}
因此,我首先创建了这个 SCSS 映射:
$themes: (
dark: (
'Franklin-Blue': #000000,
'Light-Blue': #0d0d0d,
'Pale-Blue': #2c2f33,
'Pure-White': #ffffff,
'VLight-Grey': #99aab5,
'Middle-Grey': #2c2f33,
'Dark-Grey': #23272a,
'body-color': #000000,
'text-color': #ffffff,
'bold-text': #7289da,
'White-Blue': #7289da
),
default:(
'Franklin-Blue': #1d1c4d,
'Light-Blue': #4e5d94,
'Pale-Blue': #7289da,
'Pure-White': #ffffff,
'VLight-Grey': #99aab5,
'Middle-Grey': #2c2f33,
'Dark-Grey': #23272a,
'body-color': #ffffff,
'text-color': #000000,
'bold-text': #000000,
'White-Blue': #ffffff
)
);
然后创建了这个 mixin 来循环遍历该映射并从中创建 CSS 变量:
@mixin theme() {
@each $theme, $map in $themes {
@if $theme == "default" {
:root {
@each $key, $value in $map {
#{--$key}: $value;
}
}
}
@else {
[data-theme="#{$theme}" ] {
@each $key, $value in $map {
#{--$key}: $value;
}
}
}
}
}
@include theme();
我可以看出我的做法是正确的,但它创建的 CSS 有点偏离 - 我最终得到的是:
[data-theme=dark] {
-- Franklin-Blue: #000000;
-- Light-Blue: #0d0d0d;
-- Pale-Blue: #2c2f33;
-- Pure-White: #ffffff;
-- VLight-Grey: #99aab5;
-- Middle-Grey: #2c2f33;
-- Dark-Grey: #23272a;
-- body-color: #000000;
-- text-color: #ffffff;
-- bold-text: #7289da;
-- White-Blue: #7289da;
}
:root {
-- Franklin-Blue: #1d1c4d;
-- Light-Blue: #4e5d94;
-- Pale-Blue: #7289da;
-- Pure-White: #ffffff;
-- VLight-Grey: #99aab5;
-- Middle-Grey: #2c2f33;
-- Dark-Grey: #23272a;
-- body-color: #ffffff;
-- text-color: #000000;
-- bold-text: #000000;
-- White-Blue: #ffffff;
}
如何确保引号出现在该属性选择器中,以及从 CSS 变量中删除空格?
我知道我需要将默认值移到其他主题之上才能使 :root
首先出现,但我不确定如何微调格式。
I'm currently using SCSS to attempt to recreate a CSS block. The CSS block is as follows:
:root {
--Franklin-Blue: #1d1c4d;
--Light-Blue: #4e5d94;
--Pale-Blue: #7289da;
--Pure-White: #ffffff;
--VLight-Grey: #99aab5;
--Middle-Grey: #2c2f33;
--Dark-Grey: #23272a;
--body-color: #ffffff;
--text-color: #000000;
--bold-text: #000000;
--White-Blue: #ffffff;
}
[data-theme="dark"] {
--Franklin-Blue: #000000;
--Light-Blue: #0d0d0d;
--Pale-Blue: #2c2f33;
--Pure-White: #ffffff;
--VLight-Grey: #99aab5;
--Middle-Grey: #2c2f33;
--Dark-Grey: #23272a;
--body-color: #000000;
--text-color: #ffffff;
--bold-text: #7289da;
--White-Blue: #7289da;
}
So, I've started off by creating this SCSS map:
$themes: (
dark: (
'Franklin-Blue': #000000,
'Light-Blue': #0d0d0d,
'Pale-Blue': #2c2f33,
'Pure-White': #ffffff,
'VLight-Grey': #99aab5,
'Middle-Grey': #2c2f33,
'Dark-Grey': #23272a,
'body-color': #000000,
'text-color': #ffffff,
'bold-text': #7289da,
'White-Blue': #7289da
),
default:(
'Franklin-Blue': #1d1c4d,
'Light-Blue': #4e5d94,
'Pale-Blue': #7289da,
'Pure-White': #ffffff,
'VLight-Grey': #99aab5,
'Middle-Grey': #2c2f33,
'Dark-Grey': #23272a,
'body-color': #ffffff,
'text-color': #000000,
'bold-text': #000000,
'White-Blue': #ffffff
)
);
I've then created this mixin to cycle through that map and create the CSS variables from it:
@mixin theme() {
@each $theme, $map in $themes {
@if $theme == "default" {
:root {
@each $key, $value in $map {
#{--$key}: $value;
}
}
}
@else {
[data-theme="#{$theme}" ] {
@each $key, $value in $map {
#{--$key}: $value;
}
}
}
}
}
@include theme();
I can tell I'm along the right lines, but the CSS it creates is ever so slightly off - I end up with this:
[data-theme=dark] {
-- Franklin-Blue: #000000;
-- Light-Blue: #0d0d0d;
-- Pale-Blue: #2c2f33;
-- Pure-White: #ffffff;
-- VLight-Grey: #99aab5;
-- Middle-Grey: #2c2f33;
-- Dark-Grey: #23272a;
-- body-color: #000000;
-- text-color: #ffffff;
-- bold-text: #7289da;
-- White-Blue: #7289da;
}
:root {
-- Franklin-Blue: #1d1c4d;
-- Light-Blue: #4e5d94;
-- Pale-Blue: #7289da;
-- Pure-White: #ffffff;
-- VLight-Grey: #99aab5;
-- Middle-Grey: #2c2f33;
-- Dark-Grey: #23272a;
-- body-color: #ffffff;
-- text-color: #000000;
-- bold-text: #000000;
-- White-Blue: #ffffff;
}
How do I make sure the quotes appear in that attribute selector, as well as removing the whitespace from the CSS variables?
I know I need to move the default above the other theme to make the :root
appear first, but I'm not sure how to fine tune the formatting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
总体概述一下,语法
#{…}
称为 插值 这是将动态值注入 CSS 变量(自定义属性)的唯一方法。以下是 doc 的引用:您犯的错误是将
--
放在插值大括号 ({}
) 内。这会起作用:To give a general overview, the syntax
#{…}
is called interpolation and it is the only way to inject dynamic values into a CSS variable (custom property). Here is a quote from the doc:The error you made is to put the
--
inside the interpolation curly brackets ({}
). This would work: