简体   繁体   English

CSS文件中样式为行内SVG文本的填充属性具有奇怪的行为

[英]Strange behavior with fill attribute for inline SVG text styled in CSS file

I'm having a problem with CSS styling of inline SVG code in HTML. 我在HTML中的内联SVG代码的CSS样式方面遇到问题。 I have a HTML page index.html, CSS file style.css and SVG file external.svg. 我有一个HTML页面index.html,CSS文件style.css和SVG文件external.svg。 In the SVG file I created a few hexagonal objects in symbol tags. 在SVG文件中,我在符号标签中创建了一些六角形对象。 I use this file only as library of svg objects. 我仅将此文件用作svg对象的库。 In the HTML file I use inline SVG that's using objects from the SVG file. 在HTML文件中,我使用了内联SVG,它使用了SVG文件中的对象。 And the CSS file should change color of some inline SVG text in HTML file. CSS文件应更改HTML文件中某些内联SVG文本的颜色。

This is an example of the inlined SVG code from the HTML file. 这是HTML文件中的内联SVG代码的示例。 The <use ...> tag imports objects from the SVG file. <use ...>标记从SVG文件导入对象。

<svg id="zab4text" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0,0,300,300">
  <a xlink:href="https://www.google.com" xlink:title="google">
    <use xlink:href="external.svg#hex4" fill="none"/>
    <text font-size="40" font-weight="bold" font-family="DejaVu Sans"> 
      <tspan x="55" y="105">first line</tspan>
      <tspan x="55" y="150">second line</tspan>
      <tspan x="75" y="230">third line</tspan>
    </text>
  </a>
</svg>

The CSS used to style this part of code is this: 用于样式化这部分代码的CSS是这样的:

#zab4text {
  width: 150px;
  height: 150px;
  fill: red;
}

The result should be a red text in a hexagon. 结果应为六角形的红色文本。 The hexagon is fine, but the text is black. 六边形很好,但文本为黑色。 The fill attribute has no effect on the text. fill属性对文本没有影响。

When I want to have a red text I have to give an id directly to the <text> tag and style it. 当我要显示红色文本时,必须直接给<text>标签添加一个ID并设置其样式。 Then it works. 然后就可以了。

I looked in the inspector in google chrome 55 what's happening. 我在Google Chrome 55中的检查器中查看了发生了什么。 When the fill attribute was for the svg tag as in the example, I saw in the inspector that the fill color for the text was inherited from zab4text but it didn't set the color. 在示例中,当fill属性用于svg标签时,我在检查器中看到该文本的填充颜色是从zab4text继承的,但没有设置颜色。 If the fill attribute was for the text tag, then it was his fill color and it worked fine. 如果fill属性用于文本标签,那么它就是他的填充颜色,效果很好。

Could it be some bug in google chrome? 可能是Google Chrome中的一些错误吗? When I first opened the HTML page the color was red, but when I opened the link it is pointing to, it changed color to black and I couldn't get the red back. 当我第一次打开HTML页面时,颜色是红色,但是当我打开它指向的链接时,它的颜色变成了黑色,而我却无法获得红色。 I tried it in firefox 50 and the same behavior. 我尝试在Firefox 50和相同的行为。

I know about the google chrome external file behavior, I use simple python3 web server sudo python3 -m http.server 80 . 我知道Google chrome外部文件的行为,我使用简单的python3网络服务器sudo python3 -m http.server 80

Has somebody any idea what I am making wrong or where is the problem? 有人知道我在做什么错或问题在哪里吗?

So I found the answer. 所以我找到了答案。 The line 线

<text font-size="40" font-weight="bold" font-family="DejaVu Sans">

should be 应该

<text font-size="40" font-weight="bold" font-family="DejaVu Sans" fill="inherit">

then it will take the color from the <svg> tag in all browsers I tried. 那么它将在我尝试过的所有浏览器中使用<svg>标记中的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM