[英]Can I safely use unicode characters (e.g. accents) in CSS class names or ids?
I just encountered a situation where product names are automatically pulled in to my HTML as class names, and one of those product names has an accented letter. 我刚遇到一种情况,产品名称会自动作为类名引入我的HTML,其中一个产品名称带有重音字母。 It looks like this:
它看起来像这样:
<div class="español">Hola</div>
If I add a CSS declaration with that class, like this: 如果我在该类中添加CSS声明,如下所示:
.español {background:yellow;}
Will it cause any problems? 它会引起任何问题吗? It seems to work so far, but I'm not sure if it's completely cross-browser compatible.
它似乎工作到目前为止,但我不确定它是否完全跨浏览器兼容。
Also, would it be any different if that were an id instead of a class? 另外,如果那是一个id而不是一个类,它会有什么不同吗? That seems to work so far, too, but again I am not sure if it'll hold up everywhere.
到目前为止,这似乎也有效,但我不确定它是否会在任何地方持续存在。
From the CSS specification : 从CSS规范 :
"In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_)"
“在CSS中,标识符(包括选择器中的元素名称,类和ID)只能包含字符[a-zA-Z0-9]和ISO 10646字符U + 00A0及更高,加上连字符( - )和下划线(_)”
The ISO 10646 standard and Unicode standard have synchronised their character sets ( ref ), so in this aspect they are the same. ISO 10646标准和Unicode标准已经同步了它们的字符集( ref ),因此在这方面它们是相同的。
The ñ character has the character code U+00F1 , so that is safe to use in an identifier. ñ字符的字符代码为U + 00F1 ,因此可以安全地在标识符中使用。
Apparently yes. 显然是的。 In fact, HTML 4.01 already allowed you to use Unicode characters in the
class
attribute. 事实上,HTML 4.01已允许您在
class
属性中使用Unicode字符。 Now HTML 5 allows them also on the id
attribute. 现在HTML 5也允许它们在
id
属性上。 The cool thing is that it's been tested with IE 6 and works too, so it's backwards compatible. 很酷的是,它已经通过IE 6测试并且也可以工作,因此它向后兼容。
Now what you should ask yourself is, do I really need them? 现在您应该问自己,我真的需要它们吗? In my eyes is just asking for trouble because while the W3 accepts them, some not-major browser might not support them (think browsers for the visually impaired or others).
在我看来,只是在寻找麻烦,因为虽然W3接受了它们,但一些非主流浏览器可能不支持它们(想想视障人士或其他人的浏览器)。
Read this for more info on the subject: http://mathiasbynens.be/notes/html5-id-class . 阅读本文以获取有关该主题的更多信息: http : //mathiasbynens.be/notes/html5-id-class 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.