johnnietheblack johnnietheblack - 1 month ago 4x
CSS Question

Difference between div id versus div class

What's the difference between

<div class="">
<div id="">
when it comes to CSS? Is it right 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.


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:

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>


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

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