css - CSS中的@namespace有什么用?

标签 css xml-namespaces

来自doc ,我发现使用如下命名空间:

@namespace foo url(http://www.example.com);
 foo|h1 { color: blue } 

但我想了解更多。我们为什么要使用它?

最佳答案

在您给出的示例中,color: blue规则将仅限于 h1 foo 中的元素命名空间(由 url(example.com) 链接)。

据我所知,它被认为不是经常需要的。而且它看起来确实很奇怪。

以下是其应用的一个很好的总结:http://nimbupani.com/spacing-out-on-css-namespaces.html

The only thing it defines is how to declare an XML namespace prefix in CSS. That is needed if you want to use selectors that only match elements in a certain namespace.

For example, SVG shares some common elements (e.g. <a>) and CSS properties with HTML with XML and HTML. If you are using the same stylesheet for both HTML and SVG documents, then it is best to separate out the styles for SVG and HTML to prevent any overlap. …

这是其各部分的详分割步说明:

@namespace declares the default namespace and binds a namespace to a namespace prefix. The default namespace is applied to names that do not have an explicit namespace component. … If you declare an @namespace rule with a prefix, you can use the prefix in namespace-qualified names. …

最后,这是 MDN 的文档:https://developer.mozilla.org/en-US/docs/Web/CSS/@namespace

The @namespace rule is an at-rule that defines the XML namespaces that will be used in the style sheet. The namespaces defined can be used to restrict the universal, type, and attribute selectors to only select elements under that namespace. The @namespace rule is generally only useful when dealing with an XML document containing multiple namespaces—for example, an XHTML document with SVG embedded.

The @namespace rule can be used to define the default namespace for the style sheet. When a default namespace is defined, all universal and type selectors (but not attribute selectors, see note below) apply only to elements on that namespace.

The @namespace rule can also be used to define a namespace prefix for a style sheet. When a universal, type, or attribute selector is prefixed with a namespace prefix, then that selector only matches if the namespace (and not just the name in the case of type or attribute selectors) of the element or attribute matches.

When using non-XML HTML, known elements will be automatically be assigned namespaces. This means that HTML elements will act as though they are on the XHTML namespace, even if there is no xmlns attribute anywhere in the HTML document.

Note that in XML, unless a prefix is defined directly on an attribute, that attribute has no namespace. In other words, attributes do not inherit the namespace of the element they're on. To match this behaviour, the default namespace in CSS does not apply to attribute selectors.

关于css - CSS中的@namespace有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24878905/

相关文章:

javascript - 如果在 css 中设置为 0px,如何根据内容计算像素高度?

JavaScript AddEventListener 异常

Python:哪个 XML 解析器支持 DTD !ENTITY 定义?

php - 命名空间前缀未定义。如何自动定义它/忽略错误?

twitter-bootstrap - 如何避免 twitter bootstrap 中的文本溢出?

html - 我如何划分此处附加的图像部分以支持 Bootstrap ?

xml - 何时为 xmlns 使用 URN

html - XSLT 从具有不同命名空间的 HTML 生成 xsl fo

perl - 如何使用包含命名空间声明和前缀名称的 XML::Twig 创建 XML?

javascript - Vue、Vuetify - 减少工具栏中按钮之间的空间