ID Attribute

Discussion in 'HTML' started by JohnW-Mpls, Feb 10, 2008.

  1. JohnW-Mpls

    JohnW-Mpls Guest

    Reading a message in here, I noticed the use of the attribute id
    (as in <div id="xxx">). I use css but mainly as a template for
    similarity of all pages at a site.

    For special colors or something like that for a word in a HTML
    page, I have been using <div class="xxx"> if the color is
    predefined in my .css file, and <div style="xxx"> when I need to
    spell out something special.

    Why and when would it be good to use the id attribute?


    --
    JohnW-Mpls
    JohnW-Mpls, Feb 10, 2008
    #1
    1. Advertising

  2. JohnW-Mpls

    richard Guest

    On Sun, 10 Feb 2008 11:45:09 -0600, JohnW-Mpls <>
    wrote:

    >
    >Reading a message in here, I noticed the use of the attribute id
    >(as in <div id="xxx">). I use css but mainly as a template for
    >similarity of all pages at a site.
    >
    >For special colors or something like that for a word in a HTML
    >page, I have been using <div class="xxx"> if the color is
    >predefined in my .css file, and <div style="xxx"> when I need to
    >spell out something special.
    >
    >Why and when would it be good to use the id attribute?


    Personal preferences actually.
    Just remember that a class can be used any number of times on the same
    page, as to where an ID can only be used once.
    So if you had
    <div id="xxx">text row one</div>
    <div id="xxx">text row two</div>
    would not be allowed.

    Where it may be allowed, using a class and ID name as the same, I
    refrain from doing so to avoid possible confusion.
    Such as ID="xxx" and class="xxx".
    richard, Feb 10, 2008
    #2
    1. Advertising

  3. ..oO(JohnW-Mpls)

    >Reading a message in here, I noticed the use of the attribute id
    >(as in <div id="xxx">). I use css but mainly as a template for
    >similarity of all pages at a site.
    >
    >For special colors or something like that for a word in a HTML
    >page, I have been using <div class="xxx"> if the color is
    >predefined in my .css file, and <div style="xxx"> when I need to
    >spell out something special.
    >
    >Why and when would it be good to use the id attribute?


    For example if you want to access or manipulate that element with
    JavaScript or if you want to use it as a named anchor. IDs are also
    often used for the main parts of a webpage, like #nav, #head, #content
    etc. But that's more or less just personal preference.

    Micha
    Michael Fesser, Feb 10, 2008
    #3
  4. JohnW-Mpls

    Nik Coughlin Guest

    "JohnW-Mpls" <> wrote in message
    news:...
    >
    > Reading a message in here, I noticed the use of the attribute id
    > (as in <div id="xxx">). I use css but mainly as a template for
    > similarity of all pages at a site.
    >
    > For special colors or something like that for a word in a HTML
    > page, I have been using <div class="xxx"> if the color is
    > predefined in my .css file, and <div style="xxx"> when I need to
    > spell out something special.
    >
    > Why and when would it be good to use the id attribute?


    When an element is unique. You can only legally assign an id to a single
    element. Also, an element can have multiple class values (seperated by
    space) but only one id.

    Additionally ids have a higher specifity than a class. That is, if you
    apply both a class and an id to an element, the css for the id will overrule
    that for the class.

    As an example, you will probably only ever have one page footer but you may
    have content arranged in such a way that you also have section footers. So
    one pageFooter, many sectionFooters:

    #pageFooter {
    ...
    }

    ..sectionFooter {
    ...
    }

    Generally I ask myself, am I *ever* going to possibly need more than one of
    these? If the answer is even very tentatively maybe then I use a class. If
    I am sure that it will only ever be used once, then I use an id.

    Note that by using selectors to their fullest you can get away with using
    very few id/classes in your CSS.
    Nik Coughlin, Feb 10, 2008
    #4
  5. JohnW-Mpls

    dorayme Guest

    In article <fonlcv$9ja$>,
    "Nik Coughlin" <> wrote:

    > "JohnW-Mpls" <> wrote in message
    > news:...
    > >
    > > Why and when would it be good to use the id attribute?

    >


    > Generally I ask myself, am I *ever* going to possibly need more than one of
    > these? If the answer is even very tentatively maybe then I use a class. If
    > I am sure that it will only ever be used once, then I use an id.
    >


    Well said. I tend this way too. By using an id in such cases, it
    is information to oneself (and other possible mark up source
    sticky beaks). One can be sure it was put in to mark something
    uniquely thus avoiding mistaken paths in troubleshoots over
    classes.

    --
    dorayme
    dorayme, Feb 10, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bostonasian
    Replies:
    1
    Views:
    817
    Joris Gillis
    Sep 18, 2005
  2. Replies:
    2
    Views:
    1,076
    Henry S. Thompson
    Mar 6, 2006
  3. Donnal Walter

    class attribute to instance attribute

    Donnal Walter, Jun 30, 2005, in forum: Python
    Replies:
    4
    Views:
    465
    Greg Ewing
    Jul 6, 2005
  4. Russell Warren
    Replies:
    5
    Views:
    470
    Russell Warren
    Jan 17, 2006
  5. anonymous
    Replies:
    1
    Views:
    5,955
Loading...

Share This Page