有什么原因为什么CSS不支持ids和类,从数字? [英] Is there a reason why CSS doesn't support ids and classes, starting from numbers?

查看:117
本文介绍了有什么原因为什么CSS不支持ids和类,从数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,如果id或类从数字开始,则不应用css属性。例如,以下都不会工作:

I noticed that css properties aren't applied if id or class starts from number. For example, none of following will work:

.1ww{
    /* some properties here */
}

.1{
    /* some properties here */
}


$ b b

and

#1{
    /* some properties here */
}

#1ww{
    /* some properties here */
}

为什么CSS不支持ids或类名,从数字开始? (Javascript,例如,使用数字id和类)

Why doesn't CSS support ids or class names, starting from numbers? (Javascript, for example, works with numeric ids and classes)

推荐答案

实际上,规范说,将被解释为维度[ 1 ]:

In fact, the specification states that classes starting with a number will be interpreted as a dimension [1]:


CSS2解析一个数字,紧接着一个标识符作为DIMENSION令牌(即未知单元),CSS1将其解析为一个数字和一个标识符。这意味着在CSS1中,声明'font:10pt / 1.2serif'是正确的,'font:10pt / 12pt serif';在CSS2中,在serif之前需要一个空格。 (一些UA接受第一个示例,但不是第二个。)

CSS2 parses a number immediately followed by an identifier as a DIMENSION token (i.e., an unknown unit), CSS1 parsed it as a number and an identifier. That means that in CSS1, the declaration 'font: 10pt/1.2serif' was correct, as was 'font: 10pt/12pt serif'; in CSS2, a space is required before "serif". (Some UAs accepted the first example, but not the second.)

,因为我们不知道将引入哪个维度未来,CSS中不存在的维度被解析为未知维度:

and since we don't know which dimensions will be introduced in the future, dimensions that do not exist in CSS are parsed as unknown dimensions:


在CSS1中,类名称可以以数字.55ft),除非它是一个维度(.55in)。在CSS2中,此类类被解析为未知维度(以允许添加新单元)。要使.55ft成为有效的类,CSS2需要转义第一个数字(.\35 5ft)

In CSS1, a class name could start with a digit (".55ft"), unless it was a dimension (".55in"). In CSS2, such classes are parsed as unknown dimensions (to allow for future additions of new units). To make ".55ft" a valid class, CSS2 requires the first digit to be escaped (".\35 5ft")

你可以使用从数字开始的转义第一个数字,这是根据W3C CSS验证器是有效的。请查看 plunkr

You can use class starting with numbers by escaping the first digit, which is valid according to the W3C CSS validator. check out the plunkr.

[ 1 ]附录G. CSS 2.1语法

[1] Appendix G. Grammar of CSS 2.1

这篇关于有什么原因为什么CSS不支持ids和类,从数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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