242

What's the difference between <div class=""> and <div id=""> when it comes to CSS? Is it alright to use <div id="">?

I see different developers doing this in both ways, and since I'm self taught, I've never really figured it out.

Cleptus
  • 3,446
  • 4
  • 28
  • 34
johnnietheblack
  • 13,050
  • 28
  • 95
  • 133

17 Answers17

344

ids must be unique where as class can be applied to many things. In CSS, ids look like #elementID and class elements look like .someClass

In general, use id whenever you want to refer to a specific element and class when you have a number of things that are all alike. For instance, common id elements are things like header, footer, sidebar. Common class elements are things like highlight or external-link.

It's a good idea to read up on the cascade and understand the precedence assigned to various selectors: http://www.w3.org/TR/CSS2/cascade.html

The most basic precedence you should understand, however, is that id selectors take precedence over class selectors. If you had this:

<p id="intro" class="foo">Hello!</p>

and:

#intro { color: red }
.foo { color: blue }

The text would be red because the id selector takes precedence over the class selector.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
Tyson
  • 6,214
  • 3
  • 32
  • 37
  • 17
    The reason, for those who care, that #intro has precedence is because of something called specificity: http://www.w3.org/TR/CSS2/cascade.html#specificity – Glen Solsberry Feb 13 '09 at 22:10
  • 20
    TBH, nobody use id any more unless it has something to do with integration with JavaScript (when you need an unique value). It is html leftover and you do not need to use it just because it exists. Saying that you have only one header and such so you need id instead of class might be correct. But let's say you have a search bar with style properties. If you wanted to add another search bar with the same properties in the end of page later too you couldn't, because id can only be used one once. Honestly, I just don't see any use of id. It doesn't make your code more organised or anything. – heroix Jun 07 '12 at 20:20
  • 1
    Just confused these two things for quite a long time. Now I understood that "id" should be referred with unique element while "class" can be applied into multiple elements or things according their difference – Michael Lai Jul 20 '13 at 03:03
  • 4
    I prefer to see ID in this way: if you have many elements which are alike (e.g. `
  • `in an `
      `) and you want to have a *single* one of them to behave different (whether CSS or JS doesn't matter), then you use `id` attribute.
  • – yunzen Nov 06 '14 at 08:47
  • @HerrSerker why not adding another CSS class, instead of `id` attribute? – enb081 Apr 14 '15 at 14:09
  • 2
    ID is an identifier, CLASS is a list of styling rules, They exist for different purposes – Daniel Faure Jun 04 '18 at 22:56
  • Tout a fait, Daniel. Use id for naming elements referenced in any JavaScript files via *getElementById(.)*. But always use the classname for styling since all dynamic styling overrides (e.g. after user events like hover and click or with screen size changes) are applied with class descriptors. Class descriptor styling cannot override id styling unless we use *! important* (which we shouldn't, except for debugging) so we must use classes alone for styling. – Trunk Nov 04 '19 at 13:29