首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SVG CSS转换不工作于‘CSS’元素(Chrome)

SVG CSS转换不工作于‘CSS’元素(Chrome)
EN

Stack Overflow用户
提问于 2020-07-18 13:41:11
回答 1查看 407关注 0票数 4

我无法通过CSS转换来处理SVG中的use元素。

它在最初的SVG上工作,但当我使用use时就不行了。见下文。

这似乎是特定于Chrome的(这两个例子都适用于FF)。

代码语言:javascript
复制
.stretched-rect {
  width: 50px;
  height: 50px;
}
.stretched-rect svg {
  width: 100%;
  height: 100%;
}

.stretched-rect {
  --fill-color: red;
}
.stretched-rect:hover {
  --fill-color: blue;
}
代码语言:javascript
复制
  Example with original svg (this works):
  <div class="stretched-rect">
    <svg viewbox="0 0 100 100" preserveAspectRatio="none">
      <rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
    </svg>
  </div>
  
  <br><br>
  <!-- Define SVG, so we can use it with `use` -->
  <svg style="display: none;">
    <symbol id="svg-rect" viewbox="0 0 100 100" preserveAspectRatio="none">
      <rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
    </symbol>
  </svg>
  
  Example using "use" (this does not work):
  <div class="stretched-rect">
    <svg><use xlink:href="#svg-rect"/></svg>
  </div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-18 14:22:59

正如其他人所指出的,似乎是Chrome中的一个bug。有关解决办法,请参阅下文。

正如我所评论的:您可以将样式应用于use元素,而不是在符号中设置rect样式:从符号的rect中删除样式并将其粘贴到use元素:

代码语言:javascript
复制
.stretched-rect {
  width: 50px;
  height: 50px;
}
.stretched-rect svg {
  width: 100%;
  height: 100%;
}

.stretched-rect {
  --fill-color: red;
}
.stretched-rect:hover {
  --fill-color: blue;
}
代码语言:javascript
复制
Example with original svg (this works):
  <div class="stretched-rect">
    <svg viewbox="0 0 100 100" preserveAspectRatio="none">
      <rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
    </svg>
  </div>
  
  <br><br>
  <!-- Define SVG, so we can use it with `use` -->
  <svg style="display: none;">
    <symbol id="svg-rect" viewbox="0 0 100 100" preserveAspectRatio="none">
      <rect width="100" height="100" rx="15" />
    </symbol>
  </svg>
  
  Example using "use" (this is working now):
  <div class="stretched-rect">
    <svg><use xlink:href="#svg-rect" style="fill: var(--fill-color); transition: 1s fill;"/></svg>
  </div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62969261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档