Results 1 to 2 of 2

Thread: The difference between a class and an id?

  1. #1
    Join Date
    Jan 2008
    india,kerala-god's own country

    Default The difference between a class and an id?

    The difference between a class and an id?

    Each of these terms allow you to create CSS properties that are personalised, that you can apply to tags.
    Fundamental difference:

    You can use id or class one for the other, but...

    * an id is applied to a unique object, there cannot be two same id within a page
    * a class can designate several identical objects.

    For example, it is possible to have the code below:

    <div class="test"></div>
    <div class="test"></div>

    But you can't have the following:

    <div id="test"></div>
    <div id="test"></div>

    The id must designate only one object in the document. You can define as many id you want within your CSS style sheet, but each need to identificate one element only.

    For stricter code, you must priviledge correct syntax. Use id first when you can, and class when you can't use id.

    For example: start using id by default for unique objects to make it easier to read the code. For example, give your body tag an id, then to your header block, your left block, your right block, ... but for all paragraphs or menu lists, use classes since there are several elements of this type.
    Advantages and inconvenients:

    What is the use of an id that can be used only once when you can use classes instead?

    There are several reasons for this, here are the main ones:

    * Id is practical because it more or less replaces "name" in javascript: in DHTML, objects in layers are now called with getElementById, which increases compatibility with all browsers from IE5, NN6, Opera and Mozilla. Thanks to this method, the document.layer, document.all, ... and other multiple codes will disappear to be compatible.
    * Id can be used as an anchor, since it replaces name. WE will be able to go to the top of a page like this:

    <body id="top">...
    <a href="#top">go to top</a>

  2. #2
    Join Date
    Oct 2010

    Smile Question...

    I used "id" several times, and it was ok
    do you think i'll have problems in the future?

    and thanks for the information
    I always wondered what's the difference...


Posting Permissions

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