What's the difference between CSS3's :root

2019-08-20 21:31发布

似乎无法找到这么多的信息。

碎杂志似乎本质上说, html:root都是一样的东西,但肯定必须有区别吗?

Answer 1:

From the W3C wiki:

The :root pseudo-class represents an element that is the root of the document. In HTML, this is always the HTML element.

CSS is a general purpose styling language that it can be used with other document types, not only with HTML, such as SVG.

From the specification (emphasis mine):

This specification defines Cascading Style Sheets, level 2 revision 1 (CSS 2.1). CSS 2.1 is a style sheet language that allows authors and users to attach style (e.g., fonts and spacing) to structured documents (e.g., HTML documents and XML applications).



Answer 2:

它们之间的一个技术区别是:root -是一个伪类具有比更高的特异性html (类型选择)

 :root { color: red } html { color: green; } 
 <div>hello world</div> 

因此,在上述例子中, :root选择覆盖html选择器和文本显示为红色。



Answer 3:

对于一个html文件,当然根元素是<html>标记。 但是你可以风格的CSS,现在你的SVG文档:root伪类referd到svg元素。

您可以使用CSS不仅HTML,但所有类似XML doucments,这就是为什么:root ,一般适用于根元素,无论文档类型(其中的情况下,然而在99%以上将是HTML)。



文章来源: What's the difference between CSS3's :root pseudo class and html?