简体   繁体   English

我可以在CSS类名或ID中安全地使用unicode字符(例如重音符号)吗?

[英]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.

相关问题 如何在 a.css 文件中使用在 a.scss 文件中声明的变量(例如 $blueColor:#ffffff)? - How can I use variables declarated in a .scss file (e.g. $blueColor: #ffffff) in a .css file? unicode字符的换行行为(例如🦄)? - Line breaking behaviour of unicode characters (e.g. 🦄)? 我可以在CSS类名称中使用汉字吗? - Can I use Chinese characters in CSS class names? joomla 3.2在例如菜单元素的html输出中生成css类作为CSS类的项目ID,这对我有用还是只是忽略它? - joomla 3.2 generates item ids as css classes in the html output of e.g. menu elements, is this of any use to me or just ignore it? 如何在 chrome 中关闭 css 的功能以进行测试,例如 css grid - How can I turn off features of css in chrome for testing e.g. css grid 如何修改 Wicket 活动指示器的 css(例如位置)? - How can I modify css (e.g. position) of Wicket's activity indicator? 我可以关闭CSS中的链接,例如书签安装页面吗? - Can I turn off a link in CSS, e.g. for a bookmarklet install page? 如何使我的 CSS 切换 state 变化更平滑,例如通过过渡? - How can I make my CSS toggle state changing smoother e.g. with a transition? div不能使用外部CSS文件设置样式(例如背景) - div can not be styled (e.g. background) with external css file Ajax的新手...在ajax调用后,如何将CSS恢复为默认值? (例如,重新加载CSS) - New to Ajax…how can I return my CSS to it's default value after an ajax call? (e.g. reload CSS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM