Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End pros and cons of using all Classes and no IDs?

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #27724
    mmcgu1966
    Participant

    Just joined the forum, so forgive me if this is an old debate, but while I do understand the differences between using an ID and a Class in CSS, I was wondering if there are any opinions about using ALL classes and no IDs.

    If you have multiple divs with the same class, to style them differently, you can give the second one a second class that supersedes the first class, so I’m not seeing any real reason not to use all classes.

    Is there any reason that scripts would use IDs over classes?

    -Michael

    #70004
    Chris Coyier
    Keymaster

    That’s a great question. I think the biggest factor is speed. JavaScript can find an element on a page by ID far faster and easier than it can with classes, and in fact, native JavaScript doesn’t even have a getElementByClass, only getElementByID. Probably most importantly though is that ID’s act as page anchors. So if you have a <h2 id="comments">, you can have a <a href="#comments">jump to comments</a> which jumps down to that area. Classes can’t do that, and it’s quite useful.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.