为什么我不能在CSS中使用ID选择器?

时间:2021-02-28 06:53:10

I was shocked by this line "Don't use ID selectors in CSS". Is it true? I found many posts have written this.

我被这一行“在CSS中不使用ID选择器”所震惊。是真的吗?我发现很多帖子都是这样写的。

  1. http://mattwilcox.net/archive/entry/id/1054/
  2. http://mattwilcox.net/archive/entry/id/1054/
  3. http://screwlewse.com/2010/07/dont-use-id-selectors-in-css/
  4. http://screwlewse.com/2010/07/dont-use-id-selectors-in-css/
  5. http://oli.jp/2011/ids/
  6. http://oli.jp/2011/ids/

I think we can use the ID for the selectors.

我想我们可以为选择器使用ID。

I still want to clear this up.

我还是想澄清一下。

3 个解决方案

#1


25  

I use id selectors all the time. I know which elements are going to be unique and which are not, and an id selector for unique elements (in my opinion) is best.

我一直使用id选择器。我知道哪些元素将是唯一的,哪些不是,而对于唯一元素的id选择器(在我看来)是最好的。

#2


24  

Id selectors have priority over other kinds of selectors. This can make it harder to add new rules using less specific selectors, leading to specificity wars: http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html

Id选择符优先于其他类型的选择器。这可能会使使用较少特定的选择器添加新规则变得更加困难,从而导致特定的战争:http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html。

#3


7  

It is not advisable to use CSS selectors using IDs because Then if another element in the page uses the same/similiar style, you would have to write another class.

使用IDs使用CSS选择器是不明智的,因为如果页面中的另一个元素使用相同/相似的样式,那么您就必须编写另一个类。

Even if you don't have more than one element right now , it might come later. Hence it is always advisable to use class.

即使你现在没有一个以上的元素,它可能会在以后出现。因此,使用类总是可取的。

#1


25  

I use id selectors all the time. I know which elements are going to be unique and which are not, and an id selector for unique elements (in my opinion) is best.

我一直使用id选择器。我知道哪些元素将是唯一的,哪些不是,而对于唯一元素的id选择器(在我看来)是最好的。

#2


24  

Id selectors have priority over other kinds of selectors. This can make it harder to add new rules using less specific selectors, leading to specificity wars: http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html

Id选择符优先于其他类型的选择器。这可能会使使用较少特定的选择器添加新规则变得更加困难,从而导致特定的战争:http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html。

#3


7  

It is not advisable to use CSS selectors using IDs because Then if another element in the page uses the same/similiar style, you would have to write another class.

使用IDs使用CSS选择器是不明智的,因为如果页面中的另一个元素使用相同/相似的样式,那么您就必须编写另一个类。

Even if you don't have more than one element right now , it might come later. Hence it is always advisable to use class.

即使你现在没有一个以上的元素,它可能会在以后出现。因此,使用类总是可取的。