gpt4 book ai didi

css - SVG url 的 currentColor 继承

转载 作者:行者123 更新时间:2023-11-28 10:51:07 25 4
gpt4 key购买 nike

How do I have an SVG image inherit colors from the HTML document? 相同的问题, 但特别是当应用于用作 :before 伪元素内容的 svg 图像时。

(期望的行为是两个复选标记都从主体继承红色。目前只有内联 SVG 继承。)

<style type='text/css'>
body {
color: red;
}

.checkmark {
height: 2em;
width: 2em;
}

.checkmark:before {
content: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='100%' height='100%' viewBox='-0.5 0 20 15'><rect fill='currentColor' stroke='none' transform='rotate(45 4.0033 8.87436)' height='5' width='6.32304' y='6.37436' x='0.84178'></rect><rect fill='currentColor' stroke='none' transform='rotate(45 11.1776 7.7066)' width='5' height='16.79756' y='-0.69218' x='8.67764'></rect></svg>");
}
</style>

<!-- Renders red -->
<svg xmlns='http://www.w3.org/2000/svg' width='2em' height='2em' viewBox='-0.5 0 20 15'><rect fill='currentColor' stroke='none' transform='rotate(45 4.0033 8.87436)' height='5' width='6.32304' y='6.37436' x='0.84178'></rect><rect fill='currentColor' stroke='none' transform='rotate(45 11.1776 7.7066)' width='5' height='16.79756' y='-0.69218' x='8.67764'></rect></svg>

<!-- Renders #000 -->
<div class="checkmark"></div>

jsFiddle Demo

最佳答案

content: url(...) 将 url 部分变成图像,换句话说,svg 成为它自己的独立文档。样式不适用于文档,因此在这种情况下不可能让 color 影响 svg。

当 svg 是内联的时,它是文档的 OTOH 部分,因此可以对其应用样式。

您可以制作(或动态生成)复选标记 svg 的多个版本并调整样式规则,以便选择适当的“预着色”版本。

关于css - SVG url 的 currentColor 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20509525/

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