简体   繁体   English

为什么在CSS中,#1(作为div元素的id)样式不起作用?

[英]Why in CSS, #1 (as id of a div element) styling doesn't work?

<div class="image" id="1"></div>
<div class="image" id="2"></div>
<div class="image" id="3"></div>

http://codepen.io/Chrez/pen/KdWrBe http://codepen.io/Chrez/pen/KdWrBe

As you see in the codepen the styling of "id='1'" doesn't work. 正如您在codepen中看到的那样,“id ='1'”的样式不起作用。

I need it this way because it's for slider, and my jQuery functions work this way. 我需要这种方式,因为它是滑块,我的jQuery函数以这种方式工作。

Its CSS's rule that you can't use Number as starting character or a class or id. 它的CSS规则是你不能使用Number作为起始字符或类或id。 That is why it wont work. 这就是为什么它不会工作。 You at least have to put 1 alphabet before the number! 你至少要在数字前加1个字母!

This is what said in w3c - 这就是w3c中所说的 -

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); 在CSS中,标识符(包括选择器中的元素名称,类和ID)只能包含字符[a-zA-Z0-9]和ISO 10646字符U + 00A0及更高,加上连字符( - )和下划线( _); 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 (see next item). 标识符还可以包含转义字符和任何ISO 10646字符作为数字代码(请参阅下一项)。 For instance, the identifier “B&W?” may be written as “B\\&W\\?” or “B\\26 W\\3F”. 例如,标识符“B&W?”可以写成“B \\&W \\?”或“B \\ 26 W \\ 3F”。

http://www.w3.org/TR/CSS21/syndata.html#characters http://www.w3.org/TR/CSS21/syndata.html#characters

是的,根据w3c我们不能使用#1 ,但是这个小css-hack将起作用: div[id="1"]或者只是.image[id="1"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM