When should I use classes, and when should I use id's?

Discussion in 'HTML & Website Design' started by martman, Sep 26, 2010.

  1. #1
    I'm new to CSS and just made my first site but I found that I did every single div as an id and didn't use a single class. Is this bad form? When is it appropriate to use ID's, and when should I use classes?
     
    martman, Sep 26, 2010 IP
  2. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #2
    classes = multiple elements using it
    id's = one element using it.
     
    killaklown, Sep 26, 2010 IP
  3. rajiga

    rajiga Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    First difference
    Ids are to be used only once in your html layout. Classes can be used multiple times. Generally ids are used for the main elements of the page, such as header, main content, sidebar, footer, etc. Classes are used for elements that will appear several times on your page, but can not be covered by standard html elements.

    Second difference
    Ids have priority over classes. So, if an element has a both id and class, the id will take over.

    Third difference
    Classes have no special abilities in the browser, but ID’s do have one very important trick up their sleeve. This is the “hash value” in the URL. If you have a URL like http://yourdomain.com#comments, the browser will attempt to locate the element with an ID of “comments” and will automatically scroll the page to show that element. It is important to note here that the browser will scroll whatever element it needs to in order to show that element, so if you did something special like a scrollable DIV area within your regular body, that div will be scrolled too.
     
    rajiga, Sep 27, 2010 IP