Next / Previous / Contents / TCC Help System / NM Tech homepage

5. How to write CSS rules

Most of the statements in a CSS stylesheet are rules that specify how certain elements of your page should appear.

Here is an example of a typical rule:

  h1 { color: blue;  text-align: center; }

This rule says that all h1 headings should be rendered using blue letters and centered on the page.

In general, a CSS rule has this structure:

  selector { declaration; declaration; … }

where the selector describes which kinds of elements of your Web page are affected, and each declaration describes how those elements should appear. If there are multiple declarations, separate them with semicolons (“;”).

The declaration part has this format:


where the property is a keyword that specifies what aspect of the element you are changing, and the value says what you are changing it to. In the example rule above, the selector is h1; color and text-align are properties; and blue and center are values.

You can add comments by enclosing them between “/*” and “*/” characters. For example:

   /* Render level 3 and 4 headings in red, centered, and using
    * sans-serif italic set 13/15
   h3, h4 { color: red;
            text-align: center;
            font-family: sans-serif;
            font-style: italic;
            font-size: 13pt;
            line-heading: 15pt }

See Section 7, “Selectors” and Section 9, “Declarations”.