Forums

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

Home Forums CSS IDs vs Classes

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #38745

    I have an argument with my colleague here, on whether to use ID’s (currently in this sample) or classes, inside this HEADER portion of HTML. Of course, all the items are unique and won’t appear on the same page more then once. What would you say?



    #105272
    Senff
    Participant

    Technically, if they’re all unique, it doesn’t matter. Semantically, maybe a little. For example, you could give both menus the class “menu”, and then one has ID “main-menu” and the other “additional-menu”.

    In essence, if there are elements on the page that share some properties, then bundle those properties in a class and assign that class to those elements.
    But if the look/functionality/structure of all those elements is all unique anyway, go ahead and use IDs as much as you want, I guess.

    #105289
    Schmotty
    Participant

    ID is used to define a single object on the page. Class is for classifying certain objects. (is this too technical? I feel like a know-it-all).

    Myself, I use ID mostly as a js selector, but I will style by id when I have one. But really if you have a single object on a page with a certain purpose like “main-menu” that could have a unique ID. It could also have a class of “menu”. say you want all your menu links to be the same color but the main menu is sized differently:


    #main-menu {
    width:100%;
    height:60px;
    }

    .menu a {color:red;}
    #105301
    wolfcry911
    Participant

    Why so many divs in the first place? I agree with the others, the menus may warrant a class – then again they can both be addressed with th descendant selector #header ul anyway. I’d clean it up a bit:

    And depending on the desired look, I may combine the menus into one.

    #58695
    TheDoc
    Member

    I’m not 100% sure on this, but if you end up using some sort of JS on the page I think jQuery is a tiny bit quicker when looking for IDs over classes. I could be wrong here and it most cases it wouldn’t matter (since the speed difference would be incomprehensible to most people) but it’s something to consider if you were building a massive form.

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