There are a number of ways to specify the CSS selector, which determines the HTML elements that will be affected by a specific rule.
Within a CSS rule, the selector specifies which HTML elements will be affected by the rule.
a { color: white; }
This rule will apply to all anchor (a) elements within the linked HTML code.
The same rule may be applied to more than one type of element at once, by specifying several element names, separated by commas. For example, the following rule would apply to both a elements and p elements.
p, a { color: white; }
p a { color: white; }
Contrast this to the previous CSS specifier where the element names are separated by a comma.
p.footer { font-style: italic; }
This rule will apply to any paragraph (p) element that has the attribute class="footer". It will not apply to other p elements. It will not apply to other HTML elements, even if they have the attribute class="footer".
If no HTML element name is specified, the rule will apply to all HTML elements with the appropriate class. An example is shown below:
.figure { margin-left: auto; margin-right: auto; }
This rule will apply to any HTML element that has the attribute class="figure".
p#footer { font-style: italic; }
This rule will apply to the paragraph (p) element that has the attribute id="footer". There can only be one such element within a piece of HTML code because the id attribute must be unique for all elements. This means that the HTML element name is redundant and can be left out. The rule below has the same effect as the previous rule:
#footer { font-style: italic; }
It is possible for CSS rules to conflict, i.e., for there to be more than one rule for the same element.
In general, a more specific rule, e.g., a class selector, will override a less specific one. Otherwise, the rule that is specified last wins. For example, if two CSS files are linked in the header of an HTML document and they both contain rules with the same selector, then the rule in the second file will override the rule in the first file.
Rules are also usually inherited, except where it would not make sense. For example, if there is a rule that makes the text italic for the body of an HTML document, then all p elements within the body will have italic text, unless another rule specifies otherwise. However, a rule controlling the margins of the body would not be applied to the margins of the p elements (i.e., the body would be indented within the page, but the paragraphs would not be indented within the body as well, unless they specified margins themselves).
This section describes some of the common CSS properties, including the values that each property can take.
font-family: Times, serif
This means that a Times font will be used if it is available; otherwise, the browser will choose a serif font that is available.
There are a number of relative values for size (they go down to xx-small and up to xx-large), but it is also possible to specify an absolute size, such as 24pt.
For specifying the color value, there are a few basic color names, e.g., black, white, red, green, and blue, but for anything else it is necessary to specify a red-green-blue (RGB) triplet. This consists of an amount of red, an amount of green, and an amount of blue. The amounts can be specified as percentages so that, for example, rgb(0%, 0%, 0%) is black and rgb(100%, 100%, 100%) is white, and Ferrari red is rgb(83%, 13%, 20%).
Explicit widths or heights can be either percentages (of the parent element) or an absolute value. Absolute values must include a unit, e.g., in for inches, cm for centimeters, or px for pixels. For example, within a web page that is 800 pixels wide on a screen that has a resolution of 100 dots-per-inch (dpi), to make a paragraph of text half the width of the page, the following three specifications are identical:
p { width: 50% } p { width: 4in } p { width: 400px }
These properties affect all borders, but there are other properties that affect only the top, left, right, or bottom border of an element. For example, it is possible to produce a horizontal line at the top of a paragraph by using just the border-top-width property.
This property affects all margins (top, left, right, and bottom). There are properties, e.g., margin-top, for controlling individual margins instead.
Most HTML elements are either intrinsically block-level or inline, so some uses of this property will not make sense.
Paul Murrell
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 New Zealand License.