Difference betwen id's and classes

Discussion in 'CSS' started by +:::Spider25:::+, Apr 22, 2006.

  1. #1
    What is the difference between using ID# and Class. ?

    Thanks
     
    +:::Spider25:::+, Apr 22, 2006 IP
  2. Kat2

    Kat2 Well-Known Member

    Messages:
    143
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    http://css-discuss.incutio.com/?page=ClassesVsIds

    The key thing to know is that IDs identify a specific element and therefore must be unique on the page - you can only use a specific ID once per document. Many browsers do not enforce this rule but it is a basic rule of HTML/XHTML and should be observed. Classes mark elements as members of a group and can be used multiple times, so if you want to define a style which will be applied to multiple elements you should use a class instead.

    Also, an element can have multiple class values (by space-separating the various values), while an ID must be a single value. In this way, a single element can "inherit" from multiple classes.

    The choice between ID and class can also be important from a SelectorSpecificity point of view, as IDs have higher specificity than classes. This means that, in situations where a class's properties might not be applied (for reasons of specificity and inheritance), an ID's properties probably will be -- IDs trump virtually everything, so you can be fairly sure that they'll make their mark.

    So when you're deciding whether to use class or id for your elements, consider this: is whatever property you're giving the element something that logically appear only once, or could it appear several times? In the former case, use id, in the latter, use class.
     
    Kat2, Apr 22, 2006 IP
    dct likes this.
  3. daskin sorge

    daskin sorge Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It is class if it uses it two or more times per page 1. If it is only one time, it is ID. When
    ID is used two or more times, it is likely to mis-operate in cgi.
     
    daskin sorge, Apr 23, 2006 IP
  4. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,

    Just to add a bit extra to what the guys said: you can also use multiple classes on a single element and one ID. To use multiple classes separate each class name with a single space, in the element's class attribute.

    - P
     
    penagate, Apr 23, 2006 IP
  5. +:::Spider25:::+

    +:::Spider25:::+ Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks a lot boyh of you, I will try multiple classes Penagate, Thanks a Lot
     
    +:::Spider25:::+, Apr 24, 2006 IP
  6. Tribolis

    Tribolis Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Spider, if you're using more than 2 pages - use classes! If you use only one page or only one thingy, use ID.

    ID = one person identity | Classes = more than 2 persons' identities
     
    Tribolis, Apr 25, 2006 IP