English 中文(简体)
当类名只是一个整数时,如何编写CSS选择器?
原标题:How do you write a CSS selector when the class name is just an integer?

假设我有

<span class="1">hello</span>

我想声明:

span.1 { /* rules */ }

这似乎不起作用(即CSS规则未被应用)。有没有办法让它起作用?我试着在CSS选择器中引用“1”,但似乎不是这样。

最佳回答

请查看:CSS类名/选择器中哪些字符有效

(类)名称必须以下划线(_)、破折号(-)或字母(a-z)开头

问题回答

您应该始终记住语义来命名您的类和ID。数字对任何人来说意味着什么?这算什么?

为了避免这种情况,将ID和类命名为整数并不是根据W3有效的CSS,因此大多数浏览器不支持。始终验证您的HTML和CSS。

解决方案是给你的类一个更有意义的名称。你在数什么?这是博客上的评论吗?如果是这样,你可以给每个类添加注释。如果你真的需要每个都有唯一的名称,你可以使用comment5,但作为一个类,这似乎没有多大意义,在这种情况下,你应该使用ID。

确切的命名要求也在W3C的CSS规范,第4.1.3节字符和大小写

在CSS中,标识符(包括选择器中的元素名称、类和ID)只能包含字符[a-zA-Z0-9]和ISO 10646字符U+00A0及以上,以及连字符(-)和下划线(_)它们不能以数字、两个连字符或连字符后跟数字开头。标识符还可以包含转义字符和任何ISO 10646字符作为数字代码(见下一项)。例如,标识符“B&W?”可以写成“B&W”或“B26 W3F”。

因此,您应该检查CSS语法

因此名称必须以下划线_、字母(a-z)开头,后跟任意数量的破折号、下划线、字母或数字

EDIT: I recommend to you read this article Valid chars in CSS class names. Colleague @Triptych gave us awesome answer.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...