Results 1 to 4 of 4

Thread: CSS ID vs Class

  1. #1
    Join Date
    Jan 2008
    Location
    Portland, Oregon
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default CSS ID vs Class

    Would someone please explain or direct me somewhere that explains the difference in using id or class when referencing styles? Thank you for your time and help in advance.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    ID can only be applied to one element, and is considered more specific than class.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2008
    Location
    Portland, Oregon
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Does applied to one element imply no inheritance? or that two elements can not use the same style definition... I got into the habit of using class to reference styles and just let the inheritance thing trickle down until I need to be more specific - is this bad form or practice?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    CSS means cascading style sheet. In theory the styles are supposed to trickle down or cascade. This is in fact what happens for the most part unless the style is intrinsically not inheritable, or it is contradicted by another style that takes precedence for the child element, though some styles like z-index, display, and visibility are always inherited. Some browsers carry it out slightly differently from others, but all CSS supporting browsers will cascade the styles in one fashion or another. The class selector may refer to several different elements and their children if any, while the id selector is intended to be used for only one element and its children if any per page. Regardless of which you use, its styles will, if inheritable, be inherited by its children, if any.

    Most browsers will allow multiple elements on a page with the same id for styling purposes, but it is invalid to do so according to the specifications, and it will get you into trouble sooner or later with scripts, if nothing else, so it is best to avoid it.

    It is helpful to have a reference, and experience. I like this reference:

    http://www.eskimo.com/~bloo/indexdot...pindex/all.htm

    There are many others available around the web.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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