有没有解决方法使CSS类的名称以数字开始有效? [英] Is there a workaround to make CSS classes with names that start with numbers valid?

查看:96
本文介绍了有没有解决方法使CSS类的名称以数字开始有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它在任何地方被引用的CSS类名称以数字开头不工作?
例如,我发现一个类有背景的类:

  .000000-8 {background:url /../images/common/000000-0.8.png);} 
.8FFFFFF {background:url(../../ images / common / FFFFFF-0.8.png);}

无法在大多数浏览器中使用,并且 CSS语法显示,但我的问题是有一个解决方法使CSS类的名称以数字开始有效吗? 。

解决方案

没有CSS类。问题逻辑上分为两个问题:你可以用HTML中的数字开始一个类名,并且(假设答案是是,如果是这样),如何使用CSS中的相应的类选择器? / p>

各种HTML规范对类名称施加了各种限制,但在浏览器实践中,根据HTML5,没有限制,除了类名称不能包含空格字符。因此 class = 000000-8 是有效的。



通过CSS语法规则,CSS标识符不能与未转义数字。因此,像 .000000-8 的选择器无效。但可以通过 CSS转义规则转义数字:选择器

  .\30 00000-8 

或等效地,

  .\00003000000-8 
pre>

有效,并与 class = 000000-8 匹配。



不用说,以数字开头的类名最好避免,但如果你必须使用它们(例如,因为一些HTML文档有它们,你不能改变标记),这是方式。


Is it referenced anywhere that CSS classes with names start with numbers doesn't work? for example I found that a class with background like:

.000000-8 {background:url(../../images/common/000000-0.8.png);}
.8FFFFFF {background:url(../../images/common/FFFFFF-0.8.png);}

doesn't work in most browsers I have and the CSS grammar shows that but my question is there a workaround to make CSS classes with names that start with numbers valid? .

解决方案

There are no CSS classes. The question logically splits to two questions: can you start a class name with a digit in HTML, and (assuming the answer is "yes", as it is) if it does, how do you use the corresponding class selector in CSS?

Various HTML specifications impose various restrictions on class names, but in browser practice, and according to HTML5, there are no limitations, except that a class name cannot contain whitespace characters. So class=000000-8 is valid.

By CSS syntax rules, a CSS identifier cannot being with an unescaped digit. Therefore, a selector like .000000-8 is invalid. But the digit can be escaped, by CSS escaping rules: the selector

.\30 00000-8

or, equivalently,

.\00003000000-8 

is valid and matches an element with class=000000-8.

Needless to say, class names starting with a digit are best avoided, but if you have to work with them (e.g., because some HTML documents have them and you cannot change the markup), this is the way.

这篇关于有没有解决方法使CSS类的名称以数字开始有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆