gpt4 book ai didi

css - CSS 不支持从数字开始的 ids 和 classes 是有原因的吗?

转载 作者:技术小花猫 更新时间:2023-10-29 11:13:43 24 4
gpt4 key购买 nike

我注意到如果 id 或 class 从数字开始,则不会应用 css 属性。例如,以下均无效:

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

.1{
/* some properties here */
}

#1{
/* some properties here */
}

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

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

最佳答案

事实上,规范指出以数字开头的类将被解释为维度 [ 1 ]:

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中不存在的维度被解析为未知维度:

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 .

[ 1 ] 附录 G. CSS 2.1 语法

关于css - CSS 不支持从数字开始的 ids 和 classes 是有原因的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24453303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com