简体   繁体   English

通过“all:unset”重置CSS会中断内联SVG

[英]CSS reset via „all: unset“ breaks inline SVG

When using a CSS reset like 使用CSS重置时

* {
  all: unset;
}

inline SVG graphics are not shown correctly, see https://jsfiddle.net/593qysxp/1/ 内联SVG图形显示不正确,请参阅https://jsfiddle.net/593qysxp/1/

I have tested this with Safari 11 and Chrome 61. 我用Safari 11和Chrome 61对此进行了测试。

I tried to solve this with setting the svg element to display: block or all: initial but this did not help. 我尝试通过将svg元素设置为display: blockall: initial来解决这个问题,但这没有帮助。

Does anyone has a solution? 有没有人有解决方案?

SVG 2 now defines a number of attributes as presentation attributes that were true XML attributes in SVG 1.1. SVG 2现在将许多属性定义为表示属性,这些属性是SVG 1.1中的真正XML属性。 Among them are cx, cy, rx, ry for <ellipse> elements and the d attribute for <path> elements. 其中包括<ellipse>元素的cx,cy,rx,ry和<path>元素的d属性。

That has the consequence that these, when written as attributes on the element, are treated as CSS properties. 这样做的结果是,当这些元素作为属性写入时,它们被视为CSS属性。 That is why they are overwritten by all: unset . 这就是为什么他们被all: unset覆盖all: unset (according to SVG 1.1 rules, see Addendum below.) (根据SVG 1.1规则,见下面的附录。)

That means equally that they can be stated in a style attribute, where they would have a higher specicifity than any stylesheet. 这同样意味着它们可以在style属性中声明,它们具有比任何样式表更高的特定性。

As not all browser implement these presentation attributes yet, you would have to state them for now double as attributes and style properties. 由于并非所有浏览器都实现了这些表示属性,因此您必须将它们作为属性和样式属性进行双重声明。 The result looks weird, if you ask me: 如果你问我,结果看起来很奇怪:

 * { all: unset; } head, link, meta, script, style, title { display: none; } div { display: block; } .icon { width: 4rem; } 
 <div> <svg class="icon icon--search" viewBox="0 0 20 20" version="1.1" xmlns="http://www.w3.org/2000/svg"> <ellipse cx="14.159" cy="5.87" rx="5.89" ry="5.849" style="fill:none;stroke:#000000;stroke-width:1px;cx:14.159px;cy:5.87px;rx:5.89px;ry:5.849px"/> <path d="M10,10l-9.98,10.02" style="fill:none;stroke:#000000;stroke-width:1px;d:path('M10,10l-9.98,10.02')"/> </svg> </div> 

The use of the path() functional notation is, by the way, still an open issue . 顺便说一句,使用path()函数符号仍然是一个悬而未决的问题 So this may currently work, but not for long. 所以这可能目前有效,但不会持续很长时间。 This whole technique looks like something that I would not advise to use. 这整个技术看起来像我不建议使用的东西。

Addendum: Things get further complicated by a breaking change in the SVG spec I wasn't aware of until I just read up on it. 附录:在我刚读完之前,我不知道的SVG规范发生了重大变化,事情变得更加复杂。 SVG 1.1 has this to say about the CSS cascade: 关于CSS级联,SVG 1.1有这个说法:

The presentation attributes thus will participate in the CSS2 cascade as if they were replaced by corresponding CSS style rules placed at the start of the author style sheet with a specificity of zero. 因此,表示属性将参与CSS2级联,就好像它们被放置作者样式表开头的相应CSS样式规则所取代,特征为零。

SVG 2 instead says this : SVG 2而不是说这个

Presentation attributes contribute to the author level of the cascade, following all other author-level style sheets, and have specificity 0. 表示属性有助于级联的作者级别, 遵循所有其他作者级样式表,并具有特异性0。

First or last? 首先还是最后? So far I have not encountered any browser where presentation attributes supplant author style sheet rules. 到目前为止,我还没有遇到任何浏览器,其中表示属性取代了作者样式表规则。 That includes this example. 这包括这个例子。 Following SVG 2 rules, your stylesheet rule should have been replaced by the attributes, but obviously weren't. 遵循SVG 2规则,您的样式表规则应该已被属性替换,但显然不是。

If you want to leave SVG content completely alone and not reset anything inside them, you could use 如果你想完全保留SVG内容并且不重置其中的任何内容,你可以使用

@namespace svg "http://www.w3.org/2000/svg";

:not(svg|*) {
  all: unset;
}

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

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