Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 21

Thread: ID's vs Classes

  1. #1
    Junior Member
    Join Date
    Jan 2004
    Location
    california
    Posts
    9
    Member #
    4661
    what is the difference between using a class or an ID. when should i use each?

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Always use classes when applying CSS attributes to HTML. Use IDs when using Javascript.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Senior Member Maverick's Avatar
    Join Date
    Sep 2003
    Location
    Hoboken, NJ
    Posts
    351
    Member #
    3253
    IDs and Classes can both be used for CSS.
    IDs are used only when u want to define a single, unique element in the code, such as a div that u want specific formatting for, like a footer or a content div. Classes are used to group elements in a page that will recieve the same formatting.
    So the difference is:
    ID : only one element/tag can have a given ID name.
    Class: a given class name can be used any number of times

    I disagree with filburt though, I use IDs alot for CSS formatting, to take advantage of its cascading nature. For example: If I have some navigation elements that all recieve the same formatting I might assign them all to a class with class="nav_elements", but now I want the nav element that links to the page I am currently viewing to be highlighted or something, so along with the class property, i give it an ID of id="current_page", so it now recieves the general formatting that all the nav elements, PLUS the formatting of the id tag.
    www.stevenspoker.com
    "You can't lose what you don't put in the middle, ... but you can't win much either." -Mike McDermott (Rounders)

  5. #4
    Member
    Join Date
    Jan 2004
    Posts
    94
    Member #
    4729
    so your saying you would never use an id to align text.

    lets say we have
    <div id="header">MY SITE TITLE</div>

    you wouldnt create a style to apply the formatting/text size to that style?

    personally thats what i always do, if its only going to appear once in my code i'll use an id. otherwise i'll use a class.

    Also i would never put an id on a link. (personall preference?)

  6. #5
    Senior Member karinne's Avatar
    Join Date
    Dec 2003
    Location
    Aylmer QC Canada
    Posts
    1,607
    Member #
    4335
    Liked
    8 times
    Originally posted by filburt1
    Always use classes when applying CSS attributes to HTML. Use IDs when using Javascript.
    where did you get that!?!?


    Originally posted by webmorpheus
    what is the difference between using a class or an ID. when should i use each?
    an ID can only be used once in you page, and class you can be used multiple times.

    it doesn't matter when you use 'em.
    [a web design portfolio - Currently NOT AVAILABLE for work | web design | Re-coding | PSD-to-HTML]
    I'm also on: virb - facebook - twitter - flickr - del.icio.us

  7. #6
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    i use ids more than once......

    am I doing something wrong?
    tekp :cheeky: tekponline.com

  8. #7
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    W3C doesn't allow you to use an id more than once in a VALID HTML document. This is because id's are supposed to be unique (id IS short for identification, right?).

    The reasons you will want to follow this standard are because 1) some browsers will get funky if you try to apply a CSS id style to more than one element, and 2) if you ever want to implement DOM functionality (i.e. document.getElementById() ) you're screwed. Besides, it's just a good habit to get in (coding properly, i mean)

    So, if you're applying a style to multiple elements, use CLASS, to a single element, use ID

  9. #8
    Senior Member karinne's Avatar
    Join Date
    Dec 2003
    Location
    Aylmer QC Canada
    Posts
    1,607
    Member #
    4335
    Liked
    8 times
    Originally posted by Blackdragonhide
    i use ids more than once......

    am I doing something wrong?
    and it works?!?! it shouldn't!
    [a web design portfolio - Currently NOT AVAILABLE for work | web design | Re-coding | PSD-to-HTML]
    I'm also on: virb - facebook - twitter - flickr - del.icio.us

  10. #9
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    It will work most of the time, especially in IE. However, W3C Validator is going to curse you for doing that
    There and Back Again :Ogre:

  11. #10
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    It will wind up causing problems long-term.


Page 1 of 3 1 2 3 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 07:47 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com