HTML and CSS Standards

A CSS Quick Reference

CSS uses rules to apply styles to HTML for the purpose of controlling the appearance of a Web page in a user agent (most commonly a browser, but it might be a cell phone or a screen reader). CSS can be restricted to a particular medium. Commonly supported media are all (default), screen, and print; others are not yet reliable, though I'm starting to see more media="handheld" stylesheets.

Where

What

These classification properties make it possible to change the native behaviour of tags to create special effects, e.g., making a list horizontal to use as a menu.

How

Rules

Inheritance

Most properties inherit into descendents. Thus, if you specify body { font-family: Verdana, sans-serif; }, all text in the document will follow that rule unless overridden by a more specific rule, such as h1 { font-family: Georgia, serif; }. Inheritance obviates the need for repeated specifications of many properties within class rules; in fact, many classes can simply disappear because higher level rules prevail. For instance, many people add a class to every paragraph within a div when the class could be applied to the div instead.

Normally font-size inherits, but it does not inherit into table headers or cells. See SMS Default CSS File for guidance on how to handle this hybrid.

I will add a list of properties that don't inherit when I find a reference.

!important

Properties can be labeled “!important” so as to alter the normal cascade. This is especially useful for user-defined stylesheets. An unstyled page will receive styles from a user-defined stylesheet, but a page with styles will apply those because they appear later. A user who wants particular features should mark their stylesheet color: #ff0000 !important; or equivalently. Careless use of this feature can, however, make a page unreadable or have unforeseen effects.

Comments

Comments within CSS use the same syntax as C—/* comment */. Comments may cross multiple lines and they must be closed. Failure to close a comment has unpredictable consequences.

Validation

It is more important to validate the HTML, but erroneous CSS will, of course, produce other than the desired result. Validation is not generally a severe problem, but sometimes people invent values or even properties or use a value that's not valid for that property (conversion from MSWord is a notorious offender). Correct syntax is, of course, necessary (don't forget the semicolons).

Efficiency

Very often you'll find a string of class rules like .menudiv, .menulist, and .menuitem with each being applied respectively within the <div> that contains the <ul> with its <li>s. All of these are better handled by the contextual rules .menudiv, .menudiv ul, and .menudiv li. This means you need apply a class only once; none of the contained parts needs a class, simplifying coding and maintenance, e.g., adding a new item to the menu doesn't need to have a class applied after creating it. Some <li>s may need an id for special purposes.