基于变量-SCSS动态生成CSS类
我有颜色变量(示例):
// _colors.scss
:root, * {
--color-primary-50: 1,1,1;
--color-primary-100: 2,2,2;
--color-primary-200: 3,3,3;
}
我想基于变量生成类,例如:
// _background.scss
.bg-primary-50 {
background: rgb(var(--color-primary-50));
}
.bg-primary-100 {
background: rgb(var(--color-primary-100));
}
.bg-primary-200 {
background: rgb(var(--color-primary-200));
}
如果需要更改或添加新颜色并动态填充我的_background
,我想简化我的未来修改。基于_Colors
变量的类别的文件。
似乎有很多单调的工作。有什么方法可以得到这个结果吗?也许我应该更改文件结构?
I have color variables (example):
// _colors.scss
:root, * {
--color-primary-50: 1,1,1;
--color-primary-100: 2,2,2;
--color-primary-200: 3,3,3;
}
And I want to generate classes based on the variables, for example:
// _background.scss
.bg-primary-50 {
background: rgb(var(--color-primary-50));
}
.bg-primary-100 {
background: rgb(var(--color-primary-100));
}
.bg-primary-200 {
background: rgb(var(--color-primary-200));
}
I want to simplify my future modifications if I need to change or add new colors and dynamically populate my _background
file with classes based on _colors
variables.
It seems like a lot of monotonic work. Is there any way to get this result? Perhaps I should change my file structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
@each
循环。而不是在中创建
在单个vars
:rootvar
中添加这些代替(请参见下面的示例)上述代码编译
为CSS
- 变量
CSS变量
和
use
@each
loop. Instead of creating thevars
in:root
add those in a singlevar
(see below example)the above code compiled into
And for CSS
--variables
and ???? you have CSS Variables
Like you mentioned in your comment "will this solution work for the light and dark modes?" for that you can do something like this