数值无法被CSS识别的HTML ID [英] HTML ID with numerical value not recognized by CSS
问题描述
这里是div:
< div class ='something'id ='1'>
我在页面中获得了9个不同的职位,因此在 .css
div#1.something {
code ...
}
事情是不行的,我知道这是正确的选择器,但我也试过 div.something#1
,它也不能正常工作。我认为ID是一个数字有问题,我应该改变它还是有办法?
尽管可以设置 class
或 id
以HTML5中的数字开头,但在CSS中不允许使用,请参阅规范: $ b
... ID没有其他限制条件,特别是,ID可以只包含数字,以数字开头,以下划线开头,仅包含标点符号等......
...他们不能以数字开头
strong>,两个连字符,或连字符后跟一个数字。标识符还可以包含转义字符和任何ISO 10646字符作为数字代码...
ie
< div id =1>
是有效的HTML,但您无法通过#1 { ..}
in CSS。
然而,您可以使用 [id =1] {... }
或转义它#\31 {...}
Here is the div:
<div class='something' id='1'>
I got 9 of them with different positions on page so in the .css
file I do:
div#1.something {
code...
}
And the thing is that it won't work, I know this is the proper selector but I have also tried div.something#1
and it also doesn't work, as expected. I think there is something wrong with ID as a number, should I change it or there is a way?
Although it's allowed to set a class
or id
to begin with a digit in HTML5, but it's not allowed in CSS, see the spec:
HTML5: 3.2.5.1 The id attribute
... There are no other restrictions on what form an ID can take; in particular, IDs can consist of just digits, start with a digit, start with an underscore, consist of just punctuation, etc. ...
CSS: 4.1.3 Characters and case
... they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code ...
i.e.
<div id="1">
is valid HTML, but you cannot target it with #1 {...}
in CSS.
However, you can use [id="1"] {...}
or escape it #\31 {...}
这篇关于数值无法被CSS识别的HTML ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!