Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 383
Mikul Gohil Avatar asked Nov 26 '11 14:11

Mikul Gohil


People also ask

Is it okay to use id selectors for styling purposes?

Both ID's and classes are perfectly fine to use for CSS styling.

When should you use IDs in CSS?

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

Should you use id selectors in your HTML and CSS?

The basic rule that you need to keep in mind while using classes and ids in CSS is that, id is used for single elements that appear on the page for only once (e.g. header, footer, menu), whereas class is used for single or multiple elements that appear on the page for once or more than once (e.g. paragraphs, links, ...


1 Answers

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

like image 108
Scott Simpson Avatar answered Oct 04 '22 11:10

Scott Simpson