Html 真的有人在使用 css 命名空间吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10703291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is anyone actually using css namespaces?
提问by Marcus Stade
What are the reasons that people seem to prefer techniques such as SMACSSfor namespacing, over actual css namespaces?
人们似乎更喜欢使用SMACSS等技术进行命名空间而不是实际的css 命名空间的原因是什么?
I've googled this for a bit, but I've been unable to come up with any good resources. (It makes me worried that either my google-fu is crap (very likely) or the css namespace spec is useless (less likely))
我已经在谷歌上搜索了一段时间,但我一直无法找到任何好的资源。(这让我担心我的 google-fu 是垃圾(很有可能)或者 css 命名空间规范没用(不太可能))
采纳答案by Quentin
They cover completely different use cases.
它们涵盖了完全不同的用例。
CSS namespaces are for applying CSS to XML documents that mix elements from different XML namespaces. e.g. so you can target <foo:p>
and <bar:p>
without confusion.
CSS 命名空间用于将 CSS 应用于混合来自不同 XML 命名空间的元素的 XML 文档。例如,这样您就可以定位<foo:p>
而<bar:p>
不会混淆。
SMACSS covers techniques for writing robust CSS that doesn't interfere with other parts of the page. e.g. so that .title
in your address book HTML doesn't get muddled with .title
in your list of publications HTML.
SMACSS 涵盖了编写不会干扰页面其他部分的健壮 CSS 的技术。例如,这样.title
您的地址簿中的 HTML 就不会与.title
您的出版物列表中的 HTML混淆。
Further details from the spec:
规范中的更多详细信息:
Note: In HTML, the xmlns attribute has absolutely no effect. It is basically a talisman. It is allowed merely to make migration to and from XHTML mildly easier. When parsed by an HTML parser, the attribute ends up in no namespace, not the "http://www.w3.org/2000/xmlns/" namespace like namespace declaration attributes in XML do.
注意:在 HTML 中, xmlns 属性完全没有作用。它基本上是一个护身符。只允许稍微更容易地迁移到 XHTML 或从 XHTML 迁移。当由 HTML 解析器解析时,该属性最终不会出现在名称空间中,而不是像 XML 中的名称空间声明属性那样的“ http://www.w3.org/2000/xmlns/”名称空间。
回答by John Slegers
Namespaces have a rather nasty syntax in CSS, because the ":" namespace character must be escaped by a leading backslash to differentiate it from a pseudo-class:
命名空间在 CSS 中有一个相当讨厌的语法,因为“:”命名空间字符必须用前导反斜杠进行转义,以将其与伪类区分开来:
html\:img {
border: 2px solid black;
}
html\:a:visited html\:img {
border-color: grey;
}
This is only really useful when embedding HTML inside an XML document. When adding the html namespace, elements from the HTML namespace are correctly displayed as they would appear in HTML, allowed access to capabilities that are not yet provided by CSS.
这仅在将 HTML 嵌入 XML 文档时才真正有用。添加 html 命名空间时,HTML 命名空间中的元素会像在 HTML 中一样正确显示,允许访问 CSS 尚未提供的功能。
<story xmlns:HTML="http://www.w3.org/Profiles/XHTML-transitional">
...
<restaurant>
<name>Red Apple Inn</name>
<logo>
<HTML:A href="javascript:alert('Visit the Red Apple Inn!')">
<HTML:IMG src="red-apple.gif" height="50" width="200"/>
</HTML:A>
</logo>
...
In an HTML5 context, I can't think of any cases where you would need this. The only place where I've seen namespaces in CSS so far, is Webkit's default CSS for SVG or MathML, and they use a different syntax : the @namespace
at-rule.
在 HTML5 上下文中,我想不出任何需要它的情况。到目前为止,我在 CSS 中看到命名空间的唯一地方是 Webkit 的 SVG 或 MathML 的默认 CSS,它们使用不同的语法:@namespace
at-rule。
For example, this is code from WebKit/webkit/blob/master/Source/WebCore/css/mathml.css:
例如,这是来自WebKit/webkit/blob/master/Source/WebCore/css/mathml.css 的代码:
@namespace "http://www.w3.org/1998/Math/MathML";
math {
-webkit-line-box-contain: glyphs replaced;
text-indent: 0;
direction: ltr;
}
mtext {
line-height: 1.0;
}
...
This is code from WebKit/webkit/blob/master/Source/WebCore/css/svg.css:
这是来自WebKit/webkit/blob/master/Source/WebCore/css/svg.css 的代码:
@namespace "http://www.w3.org/2000/svg";
@namespace html "http://www.w3.org/1999/xhtml";
svg:not(:root), symbol, image, marker, pattern, foreignObject {
overflow: hidden
}
svg:root {
width: 100%;
height: 100%
}
text, foreignObject {
display: block
}
...