View Full Version : CSS ID vs Class
howellsgs
01-24-2008, 06:03 AM
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.
ID can only be applied to one element, and is considered more specific than class.
howellsgs
01-24-2008, 06:16 AM
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?
jscheuer1
01-24-2008, 06:44 AM
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/css/propindex/all.htm
There are many others available around the web.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.