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

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

问题描述

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

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);}

在我拥有的大多数浏览器中都不起作用,CSS 语法 表明,但我的问题是否有解决方法 可以使名称以数字开头的 CSS 类有效?.

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? .

推荐答案

没有 CSS 类.这个问题在逻辑上分为两个问题:你能否在 HTML 中以数字开头一个类名,以及(假设答案是是",因为它确实如此)如果是,你如何在 CSS 中使用相应的类选择器?

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?

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

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.

根据 CSS 语法规则,CSS 标识符不能带有 未转义 数字.因此,像 .000000-8 这样的选择器是无效的.但是可以通过 CSS 转义规则来转义数字:选择器

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

或者,等价的,

.0003000000-8 

有效并匹配具有 class=000000-8 的元素.

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

不用说,最好避免以数字开头的类名,但如果您必须使用它们(例如,因为某些 HTML 文档有它们并且您无法更改标记),这就是方法.

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天全站免登陆