ID And Class

Discussion in 'CSS' started by gobbly2100, Jun 7, 2007.

  1. #1
    I have done lots of reading about the difference but I really don't understand it :(

    Could someone explain VERY clearly what the difference is and how I know which one I should be using?
     
    gobbly2100, Jun 7, 2007 IP
  2. Nick

    Nick Peon

    Messages:
    52
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use ID if you plan on using the style information only once and class if you plan on using it multiple times throughout a page / site.
     
    Nick, Jun 7, 2007 IP
  3. Mr Blonde

    Mr Blonde Guest

    Messages:
    142
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Like Nick said ID's should be used for unique elements on a page and only used once whereas classes can be used multiple times.

    - for example you would use IDs for a header, footer and main navigation as they would only appear once on a page.
    - and you could use classes if you wanted to use the same style in multiple instances for example if you wanted images on a page to contain a border but not all of them... you would give the ones you want to have borders the class
     
    Mr Blonde, Jun 7, 2007 IP
  4. login

    login Notable Member

    Messages:
    8,849
    Likes Received:
    349
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Why is there ID and class, why not using just one of them ?
     
    login, Jun 7, 2007 IP
  5. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Because IDs have higher specificity than classes do, and can interact with JavaScript and the DOM as well. Not only that, but they can also provide semantic meaning when used with DIV and SPAN containers as well.
     
    Dan Schulz, Jun 8, 2007 IP
  6. iWonder

    iWonder Member

    Messages:
    94
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #6
    You can say you are dividing a web page to certain group with ID and then use classes to categories it further. Adding to what Mr. Blonde said..
    - For e.g You can have ID in few main areas like Header/Sidebar/Footer/any but it can't be repeated. Its a group you're parting out.
    - You cannot have an ID inside another ID.
    - You cannot use an ID more than once in a web page.

    - Classes on the other hand can be repeated any no. of times.
    - You can have two or more classes at once For e.g <div class="myclass1 class2 urclass4">my area</div> .
    - You can categories a class for a specific ID For e.g
    #myid .myclass {
    border: 1px solid #eee;
    padding: 10px;
    }
     
    iWonder, Jun 11, 2007 IP