我试图在我的 SVG(它被设置为背景图像)中使用 CSS 变量作为填充颜色,但我很难让它工作。它显示默认黑色,但是当我检查它时,我可以看到 css 变量在那里并显示我想要的颜色。
HTML
<div class="test">
Testing the css variable color
</div>
<div class="icon">
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
:root {
--primary-color: hsl(332, 61%, 78%);
}
div {
width: 100px;
height: 100px;
}
.test {
background: var(--primary-color);
}
.icon {
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 129 129'%3E%3Cpath d='m121.3,34.6c-1.6-1.6-4.2-1.6-5.8,0l-51,51.1-51.1-51.1c-1.6-1.6-4.2-1.6-5.8,0-1.6,1.6-1.6,4.2 0,5.8l53.9,53.9c0.8,0.8 1.8,1.2 2.9,1.2 1,0 2.1-0.4 2.9-1.2l53.9-53.9c1.7-1.6 1.7-4.2 0.1-5.8z' fill='var(--primary-color)' /%3E%3C/svg%3E");
}
Run Code Online (Sandbox Code Playgroud)
我在这里看到了在 SVG 中使用了 CSS 变量,但我不确定是否可以处理背景图像?我是使用 SVG 和 CSS 变量的新手,所以我不确定我是否做错了什么......希望了解为什么它不能正确呈现颜色!
RMo*_*RMo 15
好的,我们开始......我将首先解释为什么它不起作用,然后我将展示一个替代方案。
为什么你的方法不起作用
在您的示例中,svg 不是 DOM 的一部分。所以你不能使用 css 来修改 svg 的属性。
您正在做的是向您的 url 中的 svg 添加内联样式。由于浏览器无法识别--primary-color颜色,因此它不起作用。
另一种方法
另一种方法是将 svg 放在 html 中并伪造背景。我通过绝对定位 svg 并使用 z-index 将其移动到背景来做到这一点。
请注意,您必须修改 svg 或定位以按照您想要的方式放置背景。通常,您会为此使用背景大小。但是通过一些努力,您可以在 svg 中复制此行为或使用 css 更好地定位它。
:root {
--primary-color: hsl(332, 61%, 78%);
}
div {
width: 100px;
height: 100px;
}
.test {
background: var(--primary-color);
}
.icon{ /*postion relative for absolute positioning to work*/
position: relative;
}
.icon>svg{
position: absolute;
top: 0px;
right: 0px;
left: 0px;
bottom: 0px;
z-index: -1;
}
.icon>svg>path{ /*target the image with css*/
fill: var(--primary-color);
}Run Code Online (Sandbox Code Playgroud)
<div class="test">
Testing the css variable color
</div>
<div class="icon">
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 129 129' id='background'><path d='m121.3,34.6c-1.6-1.6-4.2-1.6-5.8,0l-51,51.1-51.1-51.1c-1.6-1.6-4.2-1.6-5.8,0-1.6,1.6-1.6,4.2 0,5.8l53.9,53.9c0.8,0.8 1.8,1.2 2.9,1.2 1,0 2.1-0.4 2.9-1.2l53.9-53.9c1.7-1.6 1.7-4.2 0.1-5.8z'/> </svg>
<p>Text goes here...</p>
</div>Run Code Online (Sandbox Code Playgroud)
小智 11
现在您可以将 SVG 包含为掩码图像而不是背景。不要忘记从 .icon 块的 css 变量设置背景颜色。
.icon {
background: var(--primary-color);
-webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 129 129'%3E%3Cpath d='m121.3,34.6c-1.6-1.6-4.2-1.6-5.8,0l-51,51.1-51.1-51.1c-1.6-1.6-4.2-1.6-5.8,0-1.6,1.6-1.6,4.2 0,5.8l53.9,53.9c0.8,0.8 1.8,1.2 2.9,1.2 1,0 2.1-0.4 2.9-1.2l53.9-53.9c1.7-1.6 1.7-4.2 0.1-5.8z' /%3E%3C/svg%3E");
mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 129 129'%3E%3Cpath d='m121.3,34.6c-1.6-1.6-4.2-1.6-5.8,0l-51,51.1-51.1-51.1c-1.6-1.6-4.2-1.6-5.8,0-1.6,1.6-1.6,4.2 0,5.8l53.9,53.9c0.8,0.8 1.8,1.2 2.9,1.2 1,0 2.1-0.4 2.9-1.2l53.9-53.9c1.7-1.6 1.7-4.2 0.1-5.8z' /%3E%3C/svg%3E");
}
Run Code Online (Sandbox Code Playgroud)
Gir*_*a C 10
不要包含 svg 作为背景,这样做你无法控制它的填充,而是尝试在 html 中内联添加它,并通过 css 你可以通过 css 变量控制填充,请检查下面的工作示例,希望它有帮助:)
:root {
--primary-color: hsl(332, 61%, 78%);
}
div {
width: 100px;
height: 100px;
}
.test {
background: var(--primary-color);
}
.icon {
color: var(--primary-color);
fill: currentColor;
width: 64px;
height: 64px;
}Run Code Online (Sandbox Code Playgroud)
<div class="test">
Testing the css variable color
</div>
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 129 129">
<path d="m121.3,34.6c-1.6-1.6-4.2-1.6-5.8,0l-51,51.1-51.1-51.1c-1.6-1.6-4.2-1.6-5.8,0-1.6,1.6-1.6,4.2 0,5.8l53.9,53.9c0.8,0.8 1.8,1.2 2.9,1.2 1,0 2.1-0.4 2.9-1.2l53.9-53.9c1.7-1.6 1.7-4.2 0.1-5.8z"/>
</svg>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22469 次 |
| 最近记录: |