gpt4 book ai didi

css - 使用转义字符将值从 mixin 传递到类

转载 作者:太空宇宙 更新时间:2023-11-04 07:00:51 25 4
gpt4 key购买 nike

我有一个贴图生成器,它添加了一个后粒子:

@mixin generator {
@each $key, $value in $config {
$infix: '\@#{$key}' !global;
@content
}
}

我在多种情况下使用上面的 mixin 在末尾添加粒子,例如:

.y- {
@include generator {
&-n#{$infix} {
display: none !important;
}
&-in#{$infix} {
display: inline !important;
}
}
}

我收到以下错误:

Invalid CSS after "&-none": expected selector, was "@"\n

我想要的是:

.y-n@key_name1{
display: none !important;
}

.y-n@key_name2{
display: none !important;
}

这仅在我使用 \@ 时出现。

我可以在创建类时添加这个粒子,但就像我说的那样,我想在多种情况下使用它并根据条件改变粒子,所以我需要在 mixin 中。

最佳答案

所以这有效...

$config: (
1: 'a',
2: 'b',
3: 'c'
);

@mixin generator {
@each $key, $value in $config {
$infix: '-#{$key}' !global;
@content
}
}

.y- {
@include generator {
&-n#{$infix} {
display: none !important;
}
&-in#{$infix} {
display: inline !important;
}
}
}

查看此 sassmeister https://www.sassmeister.com/gist/324c3c0003a91eb676b00dfe1877cae0

但是一旦你添加..

\@

..到你的 $infix 变量键字符串,它会抛出一个错误!

$infix: '\@#{$key}' !global;

我假设是 \@ 破坏了你的 sass/css。

如果您看到 What is the purpose of the '@' symbol in CSS?问题你只能使用 @ 符号来表示(例子)...

/* Import another stylesheet from within a stylesheet */
@import url(style2.css);

/* Apply this style only for printing */
@media print {
body {
color: #000;
background: #fff;
}
}

/* Embed a custom web font */
@font-face {
font-family: 'DejaVu Sans';
src: local('DejaVu Sans Regular'), url(/fonts/DejaVuSans.ttf);
}

也许放弃 \@ 并且你的 sass 会正常运行?

关于css - 使用转义字符将值从 mixin 传递到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52042410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com