CSS Classes and ID's
Why should a particular style be specified only for particular tags? Classes and ID's make it possible to specify a particular style for a variety of tags. With classes and ID's, you can also define different styles for the same HTML tag.
This tutorial focuses on:
- Classes
- ID's
Classes
With classes, you can specify different styles for the same HTML tag.
For example, you can specify two paragraphs to have different color text:
To specify a tag as part of a class, you have to use the class attribute within that tag:
Styles for classes are specified with the . (dot) character followed by the class name in an internal or external stylesheet.
In the above example, the class text is specified to have a font type of tahoma, a font size of 16 pixels, to be bold, and green. Any tag specified as part of the text class will get these properties.
Based on the stylesheet from above:
This text will be green, bold, tahoma, and have a font size of 16 pixels
NOTE: Begin class names with letters only, not numbers. Class names that begin with numbers do not work in Mozilla/Firefox browsers.
ID's
ID's are specified with the # (pound sign) followed by the ID name in an internal or external stylesheet.
In this example, the text ID is specified to have a typeface of georgia, a font size of 18 pixels, to be bold, and blue. Any tag specified as part of the text ID will get these properties.
Based on the stylesheet from above:
This text will be blue, bold, georgia, and have a font size of 18 pixels
NOTE: Begin ID names with letters only, not numbers. ID names that begin with numbers do not work in Mozilla/Firefox browsers.