Results 1 to 10 of 10

Thread: Div with ID or CLASS? confused

  1. #1
    Join Date
    Mar 2007
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Div with ID or CLASS? confused

    Hi

    Whats the difference between id & class in a div tag?
    Which one should be used and in what situation?


    Thanks for the info on this.

    Shyn3

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    id's are used when it will only be applied to ONE div.
    classes are used when it's required more than once.

    Example:

    HTML Code:
    <div id="onlyone"></div>
    <div class="multipleones"></div>
    <div class="multipleones"></div>
    <div class="multipleones"></div>
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by alexjewell View Post
    id's are used when it will only be applied to ONE div.
    classes are used when it's required more than once.
    Yes, and remember to think ahead: just because there might only be one now, there may be more in the future. Use the id attribute when something is truely unique.

    There is an exception: if the element needs to be accessed from a script, assigning it an id attribute value makes obtaining a reference to the element much easier.
    Mike

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Quote Originally Posted by mwinter View Post
    There is an exception: if the element needs to be accessed from a script, assigning it an id attribute value makes obtaining a reference to the element much easier.
    True, mwinter, but you still don't want to use an id more than once. If I recall, there's a thing in javascript that calls things by their class, too. getElementsByClass('classhere') or something?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by alexjewell View Post
    True, mwinter, but you still don't want to use an id more than once.
    Quite, and I hope I didn't imply otherwise. An id attribute value must be completely unique within a document; it cannot be duplicated.

    If I recall, there's a thing in javascript that calls things by their class, too. getElementsByClass('classhere') or something?
    No, there isn't. However, one can write such a function with the aid of the getElementsByTagName method.
    Mike

  6. #6
    Join Date
    Mar 2007
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks alot makes alot sense to me now.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Doesn't class also affect CSS?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    Doesn't class also affect CSS?
    Yes, class names can be used in selectors. A class (or pseudo-class) is more specific than an element name, but less specific than id attribute values and in-line style declarations.
    Mike

  9. #9
    Join Date
    Mar 2007
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    One more question. In my page I have everything with class, there is no id attribute. i have two columns, left column is a menu and right menu is the content and sometimes when I access the page it messes up the content column and shifts it position to the bottom of left column. Someone suggested I rename the class name and I tried that too but it didn't work. What could be causing this??

  10. #10
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shyne View Post
    One more question. In my page I have everything with class, there is no id attribute. i have two columns, left column is a menu and right menu is the content and sometimes when I access the page it messes up the content column and shifts it position to the bottom of left column.
    It's possible that the combined width of each column exceeds the width of the containing element.
    Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •