Why shouldn't I use ID selectors in CSS? [clos

2019-01-17 19:50发布

问题:

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

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

I think we can use the ID for the selectors.

I still want to clear this up.

回答1:

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



回答2:

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.



回答3:

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.

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