What is the difference between Div Id and Div Class?

Discussion in 'CSS' started by scripts, Sep 8, 2007.

  1. #1
    what is the main difference between div id and div class? i think they can do the same work, and i think there is no difference between them, Please tell me what make them different? and how they effect in design? what should we use more?
     
    scripts, Sep 8, 2007 IP
  2. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There should only be one occurrence of a unique ID value per page, but there can be an unlimited number of occurrence of a class.
     
    norfstar, Sep 8, 2007 IP
  3. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #3
    And to go further, use a class for a set of css styles that are to be used for more than one thing on the page.

    I use classes for things that otherwise do the same thing. For instance, here's a list:

    <ul>
    <li>Foo</li>
    <li>Bar</li>
    <li>Lisa</li>
    <li>1337</li>
    <li>Caturday</li>
    <li>Bart</li>
    </ul>

    Da boss tells me he wants all the geek-terms in red and the normal words blue.
    Instead of giving Lisa and Bart different ids, I might as well give them classes, cause then I can just have one class in my css. Id's in this case are more work (yes, I've just been watching Simpsons).

    <ul>
    <li>Foo</li>
    <li>Bar</li>
    <li class="normalname">Lisa</li>
    <li>1337</li>
    <li>Caturday</li>
    <li class="normalname">Bart</li>
    </ul>

    CSS:
    ul li {
    color: red;
    }

    ul li.normalname {
    color: blue;
    }

    See, I could use the class more than one time, and if I later add "Moe" I can also give him the same class. Notice I didn't name the class "blue." The class should describe what needs the class not the style : )

    I use Ids on my divs because I really never use the same style on two divs. I generally only ever have one header, one maincontent, one footer... I do sometimes have multiple menus, but I generally give each menu an id, and use classes within as above.
     
    Stomme poes, Sep 8, 2007 IP
  4. just-4-teens

    just-4-teens Peon

    Messages:
    3,967
    Likes Received:
    168
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i try to use id for formating the page

    <div id="header"></div>

    and use class for styles

    <a href="#" class="styled-links"></a>
     
    just-4-teens, Sep 8, 2007 IP
  5. tarponkeith

    tarponkeith Well-Known Member

    Messages:
    4,758
    Likes Received:
    279
    Best Answers:
    0
    Trophy Points:
    180
    #5
    I agree 100% with Stomme poes and just-4-teens...

    ID's are for when there's 1 instance of something on your site (like a sidebar or footer), and CLASSes are for when there's multiple, like links...
     
    tarponkeith, Sep 8, 2007 IP
  6. mikebrad0927

    mikebrad0927 Peon

    Messages:
    266
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    In order for your page to validate, you can only have what stomme and just4 are saying, 1 instance of each id per page.
     
    mikebrad0927, Sep 8, 2007 IP
  7. bnandika

    bnandika Well-Known Member

    Messages:
    1,079
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    140
    #7
    I get this from htmldog

    " The difference between an ID and a class is that an ID can be used to identify one element, whereas a class can be used to identify more than one."
     
    bnandika, Sep 9, 2007 IP
  8. scripts

    scripts Banned

    Messages:
    63
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think Bnandika Definition really precise and easy, thanks to all of guys who tried to help me in understanding the difference between id and class..:)
     
    scripts, Sep 10, 2007 IP
  9. RedMatrix

    RedMatrix Guest

    Messages:
    69
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Also, you can have multiple classes on an element, but never multiple ID's.

    <p class="help" class="screen"></p>
     
    RedMatrix, Sep 11, 2007 IP
  10. tarponkeith

    tarponkeith Well-Known Member

    Messages:
    4,758
    Likes Received:
    279
    Best Answers:
    0
    Trophy Points:
    180
    #10
    But I'm pretty sure they're usually written like:
    <p class="help screen">
    
    HTML:
     
    tarponkeith, Sep 11, 2007 IP
  11. RedMatrix

    RedMatrix Guest

    Messages:
    69
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    why, sure!
     
    RedMatrix, Sep 12, 2007 IP
  12. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #12
    As mentioned, you should only have one of any given ID per page. To clarify, that does not mean per type of element (e.g., you can't do id="blah" for a 'p' tag and later for a 'div' tag). And ID also acts as an anchor, so you can link to #ID on your pages.

    Classes can be assigned to multiple elements and multiple classes can be assigned to each element.

    Keep in mind, you can also use classes and ID's with JavaScript. For example, it's very easy to hide/show a div based on ID, but it's also possible to hide/show all divs with a specific class. This is much easier with Prototype :)
     
    Gordaen, Sep 12, 2007 IP