Cascading Style Sheets (CSS) provide a simplified and expansive set of styles for HTML elements, describing all aspects of borders, margins, fonts, and text positioning.

Style declarations have a simple, regular pattern and a comprehensive application.


Stylizing Web Pages

Cascading Style Sheets (CSS) are a comprehensive approach to adding stylistic enhancements to web pages.

CSS lets you redefine the default stylistic behavior of HTML’s structural elements in a way that you can easily use in multiple web pages.

Zen Garden: The Beauty of CSS Design provides multiple examples of how styles might change while the basic structure of a document persists.


Style Declarations

CSS styles are specified with a simple, regular pattern that is distinct from that used by HTML elements.

The most basic part of a style description is the declaration, which consists of a property: value list sequence:

property: value1 value2 ...

For example, to create a solid border around an element, one would use the following declaration:

border: solid

Some properties accept multiple values, and they are separated by spaces:

border: solid red

A set of declarations can be listed together by separating them with semicolons:

property1: value1; property2: value2; property3: value3; ...

To make editing easier, it’s a good habit to always include a semicolon on the last property, though it is not required:

font-style: italic; text-align: right; border: solid red;

The Style Attribute

Given a set of style declarations, the most direct way to apply them to HTML elements is the style="declarationset" attribute of a particular HTML element, for example:

<p style="font-style: italic; text-align: right; border: solid red;"> This paragraph will have a boxed, right-oriented formatting.</p>

This paragraph will have a boxed, right-oriented formatting.

You should use the style= attribute judiciously, however, as it buries the style declarations deep in a document where they can be difficult to notice and compare or apply to other elements.

We will shortly see several approaches that are generally better.


2.0. Exercises

  1. In Dreamweaver’s Code View, add a style= attribute to one of the elements in your class.html, using some of the properties given above and any obvious variants.
  2. Switch to your browser and open/reload the file to see the effect of the style= attribute.

A style sheet is a collection of rules describing how styles should be applied in an HTML document.


Style Rules

Style declarations can be given a general scope through a sophisticated set of style rules that describe how to apply them to HTML tags.

A style rule consists of a set of declarations grouped inside of curly braces, preceded by a selector that distinguishes it from any other declaration set and defines the scope of its application:

selector { declarationset }

We saw an example earlier that applies to all paragraphs in a document:

p { font-style: italic; text-align: right; border-style: solid; }

There are several ways to define selectors, and we will discuss them shortly.


Style Sheets

Style rules are commonly contained within the HTML element <style>....</style> and placed in the document header:

<head>
   ....
    <style type="text/css">

        selector1 { declarationset1 }
        selector2 { declarationset2 }
        selector3 { declarationset3 }
        ....
    </style>
    ....
</head>

Such a collection of rules is called a style sheet.

The type= attribute is required to let the browser know that the style sheet contains text, and the particular type of style is CSS (yes, there are others!).

Collecting style rules together in one or a few style sheets is generally a good idea, for ease of editing as well as keeping track of them.


External Style Sheets

Quite often a web site will consist of multiple pages, and to ensure a consistent style across them it’s a good idea to use an external style sheet that can be referenced by all of them.

Such a style sheet is simply a file full of style rules, without any HTML such as the <style> tag.

Usually it has an extension of .css, and will reside near the root of a web site for easy access by all files.

To reference an external style sheet in a document, use the stand-alone <link> element in the document’s header:

<head>
    ....
    <link rel="stylesheet" type="text/css" href="../styles.css">

    ....
</head>

The relationship attribute rel= specifies the purpose of the link, while the type= attribute is the same as for the <style> element.

The href= attribute is a URL just as for the anchor element, but here it loads in the style sheet from a local or remote location, rather than leading away to another web page.


Style Comments

You can include comments in your style sheets by enclosing them between /* and */:

<style type="text/css">
        selector1 { declarationset1 } /* This comment will be ignored */
    ....
</style>

Comments can extend across multiple lines, and are a quick way to turn off a particular style rule or rules.


2.1. Exercises

  1. In Dreamweaver’s Code View, add a <style> element to the header of your class.html, and include the paragraph rule above.
  2. Switch to your browser and open/reload the file to see the effect of the style sheet on all paragraphs.
  3. Now save the style sheet rule in an external style sheet class.css, and replace the <style> element with a <link> element referencing the file.
  4. Again switch to your browser and verify that the new style sheet is still acting within the document class.html.

HTML Selectors provide a great deal of flexibility in applying styles to a document.


The Identifier Selector

The first type of selector we will consider is the identifier selector, which applies a particular style rule to an element with a unique identifier attribute id=.

By beginning a rule with a hash (#) and the identifier name, the declaration will apply only to the element with that identifier:

<style type="text/css">
    #longcomment { font-style: italic; text-align: right; border: solid red; }
</style>
....
<p id="longcomment">
This paragraph will have a boxed, right-oriented formatting.</p>

This paragraph will have a boxed, right-oriented formatting.

Note that the syntax for an identifier selector is the same as that used to search for a particular identifier within a document.

Recall that identifiers must begin with an ASCII letter [A-Za-z] but after that they may also include digits [0-9], hyphens [-], underscores [_], colons [:], and periods [.].; they are case-sensitive.

Question: Why is using an element’s id= attribute usually preferred to using a style= attribute?


The Class Selector

Somewhat more general than the identifier selector is the class selector, which applies a particular style rule to all elements that choose to reference it with the class= attribute.

The rule is defined by beginning the class name with a . rather than a #:

<style type="text/css">
    .comment { font-style: italic; text-align: right; border: solid red; }
</style>
....
<p class="comment">
This paragraph will have a boxed, right-oriented formatting to set it off from the main text.</p>
<h3 class="comment">
This heading will be the same.</h3>

This paragraph will have a boxed, right-oriented formatting to set it off from the main text.

This heading will be the same.

Note that a class can be referenced by different types of HTML elements, which retain any of their default styling that doesn’t conflict with the class; this is an example of a style cascade, which will be discussed shortly.

Question: Which two HTML elements would you use if you didn’t want to work around predefined styles?

For maximum compatibility, it’s adviseable to begin class names with an ASCII letter [A-Za-z], but after that they may also include digits [0-9], hyphens [-], underscores [_], colons [:], and periods [.].

Be careful: class names are case-sensitive!

Question: Why is it a good idea to give names to classes that indicate their purpose, rather than their style, e.g. comment rather than redbox?


The Element Selector

Even more general than the id or class selectors is the element selector, which applies to any given type of HTML element, no matter how many times it appears in the document.

An element selector simply consists of the element name, without enclosing brackets <> or other punctuation:

<style type="text/css">
    p { font-style: italic; text-align: right; border: solid red; }
</style>
....
<p>
All paragraphs now will have a boxed, right-oriented formatting.</p>
<p>
Yes, all of them.</p>

All paragraphs now will have a boxed, right-oriented formatting.

Yes, all of them.


The Universal Selector

As its name implies, the universal selector is the most general way to apply styles; it affects every HTML element in the document.

A universal selector is simply designated by an asterisk (*):

<style type="text/css">
    * { font-style: italic; text-align: right; border: solid red; }
</style>
....
<p>
Every element will now have the same style.</p>
<ul><li>
That means this one.</li><li>And this one.</li></ul>
<table><tr><td>
And this one, too!</td></tr></table>

Every element will now have the same style.

And this one, too!

Be careful how you use the universal selector; it can easily have unintended side effects!


Group Selectors

Multiple selectors of any type can be grouped so that they all apply the same declaration set.

This can be convenient for establishing consistency between, for example, all header elements.

The syntax is to list them together, separated by commas:

<style type="text/css">
    p, .comment, #shortcomment { font-style: italic; text-align: right; border: solid red; }
</style>
....
<p>
This paragraph will have a boxed, right-oriented formatting.</p>
<h3 class="comment">
This header will be the same.</h3>
<h2> And following this <span id="shortcomment">the formatting will be the same, too.</span></h2>

This paragraph will have a boxed, right-oriented formatting.

This header will be the same.

And following this the formatting will be the same, too.


Property Inheritance

Much of the convenience of CSS is due to the fact that properties are often inherited by descendant elements.

So, for example, if you set some text properties for paragraphs, and a paragraph contains a phrase or anchor or other element, then those properties will be inherited and the text will be consistently displayed inside those descendants (assuming they apply minimal changes themselves):

<style type="text/css">
    p { color: navy; }
    em { font-style: italic; }
    strong { font-weight: bold; }
</style>
....
<p>
This paragraph will have navy text, <em>even inside an emphasized phrase that contains <strong>an even stronger emphasized phrase</strong></em>.</p>

This paragraph will have navy text, even inside an emphasized phrase that contains an even stronger emphasized phrase.

Inheritance will depend on the property in question; for example, padding, border, and margin properties are not inherited automatically.

The keyword inherit can be used with any property, and specifies that an element’s property value will be the same as for its parent element:

<p style="padding: 1em;">
    This entire paragraph has padding, and
    <span style="color: green;">this phrase will have the usual spacing</span>, but
    <span style="color: green; padding: inherit;">this phrase will have its own padding</span>
    on either side.
</p>

This entire paragraph has padding, and this phrase will have the usual spacing, but this phrase will have its own padding on either side.


2.2. Exercises

  1. In Dreamweaver’s Code View, edit your class.css, and add examples of identifier, class, and element rules.
  2. Now open the document class.html and add identifier and class attributes to existing elements in this document.
  3. Switch to your browser and open/reload the file to see the effect of these styles; are any of the properties inherited?
  4. Returning to Dreamweaver’s Code View, edit your class.css, and look for any independent selectors that can be grouped together; if there are none, add a selector to some rule, and an HTML element to match.
  5. Switch to your browser and open/reload the file to see the effect of these styles.

Multiple style rules often conflict, but CSS has a well-defined procedure to assign precedence.


Precedence Overview

You may have realized by now that with a large number of CSS rules floating around, and with multiple ways to apply them, it’s very likely that some of these rules will overlap, intentionally or accidently (particularly when external style sheets are referenced).

If the rule declarations are complementary (e.g. one applies to fonts while the other applies to borders), then CSS simply merges them.

But if there are conflicts, e.g. about which font to use, CSS has well-defined procedures to resolve the issue.

CSS assigns precedence to style rules based on:

  1. origin — who created the styles;
  2. specificity — how closely tied the style rules are to the elements they affect;
  3. sequence — the order in which the style rules appear in a style sheet.

These are listed from most to least important.

This process of ”cascading styles” is what gives CSS its name.

What follows is a general guide; the actual cascade procedure is fairly complex though it usually ”makes sense”.


Origin Precedence

As you are probably aware, browsers make many style choices themselves, e.g. what fonts to use for text, how large a paragraph’s margins should be, etc.

Some of these default styles can usually be modified through the browsers’ preferences.

You may not be aware, however, that most browsers also let readers incorporate their own style sheets, which apply to all web pages they read (e.g. to always increase the font size).

And then there are the styles an author defines for a web page, as we’ve been discussing here.

For these three origins of styles, the precedence order, from highest to lowest, is:

  1. author styles;
  2. reader styles;
  3. browser preferences;
  4. browser defaults.

Specificity Precedence

When style rules have the same origin, CSS tries to distinguish them based on their specificity.

We’ve previously seen several ways that styles can be applied to HTML elements.

In terms of how specific they are to the element they modify, style rules can be organized from most specific to least specific:

  1. style element attribute
  2. #identifier selector
  3. .class selector
  4. element selector
  5. universal (*) selector
  6. inherited properties

This is also the order of precedence, from highest to lowest, as we can see in the following example:

<style type="text/css">
    #cascade {                            text-align: center; border: dotted orange; }
    .comment {        font-style: italic; text-align: right;  border: solid red; }
    p { color: black; font-style: normal; text-align: left;   border: none; }
</style>
....
<p style="border: dashed yellow;" id="cascade" class="comment">
This paragraph will have yellow-dash-boxed, centered, italic black text, <span style="color: red;">except for this part</span>.</p>

This paragraph will have yellow-dash-boxed, centered, italic black text, except for this part.


Sequence Precedence

When style rules have the same origin and specificity, CSS distinguishes them based on their sequence in a style sheet.

In particular, precedence is always given to the later rule in a style sheet:

<style type="text/css">
    p {               font-style: italic; text-align: right; border: solid red; }
    p { color: black;                     text-align: left;  border: none; }
</style>
....
<p>
This paragraph will have no border, left-oriented formatting, and black italic text.</p>

This paragraph will have no border, left-oriented formatting, and black italic text.

By definition, an external style sheet is always considered to be earlier in sequence than a document’s internal style sheet.

Question: What do you think would happen if a single declaration set repeated a style declaration, for example:

p { color: black; color: green; }

2.3. Exercises

  1. In Dreamweaver’s Code View, review your class.css; are any of the style rules conflicting?
  2. If none of your rules conflict, add some that are.
  3. Switch to your browser and open/reload your class.html. Do the styles displayed make sense?

Property values can be keywords but are often numeric, with explicit or implied units.


Keyword Property Values

In the previous examples, we have seen many style properties that have values which are keywords, e.g.

font-style: italic; text-align: right; border: solid red;

The allowed keywords are specific to the property; sometimes the same keyword is used for more than one property, but be careful because it could have a very different meaning!


Numeric Property Values

Many properties take numeric values, e.g.:

border: solid 5px; font-size: 0.2in; line-height: 150%; color: #00FFCC;

Numeric values can be:

Numeric values can be negative, but many properties place limits on the range of allowed values.

Numeric values always have a unit associated with them, which describes the scale of the numbers.

Sometimes that unit is spelled out, as in the border and font-size properties above, and other times it is implied, as in the line-height and color values.


Percentage Values

Often (but not always), numeric property values can be expressed as percentages:

width: 50%; font-size: 120%; line-height: 150%;

Generally speaking, percentages are simply fractions (% = 1/100), but in CSS they are always fractions of something, for example:


Colors

As we have seen, CSS can color the text, borders, and backgrounds of elements.

Colors are commonly specified by a keyword representing a common color, e.g. red, olive, or navy (more are listed as comments below).

Better control is provided by a color model in which most colors your eye can see are specified by a triplet of three values describing the amount of red (R), green (G), and blue (B) they contain.

The three RGB values each use a decimal unit for which 0 represents no color and 255 is fully saturated color:

color: rgb(255,  0,  0); /* red  */
color: rgb(  0,255,  0); /* lime */
color: rgb(  0,  0,255); /* blue */

Combinations of these three RGB values produce the other colors you are familiar with, for example:

color: rgb(255,255,  0); /* yellow  */
color: rgb(  0,255,255); /* aqua    */
color: rgb(255,  0,255); /* fuchsia */
color: rgb(255,255,255); /* white   */
color: rgb(128,128,128); /* gray    */
color: rgb(  0,  0,  0); /* black   */

Hexadecimal Color Specifications

The reason the color scale is based on 255 is primarily historical, as it corresponds to one byte (8 bits) of data, and can easily be represented as a hexadecimal number, since 255 = #FF.

CSS values can also be specified as a single number that varies between 0 and 255*255*255 = 16581375.

This value must be in hexadecimal, however, which ranges from 0 to #FFFFFF, with each pair of hexadecimal digits representing red, green, and blue in sequence.

The format is therefore fairly easy to interpret:

color: #800080; /* purple = rgb(128,  0,128) */
color: #800000; /* maroon = rgb(128,  0,  0) */
color: #808000; /* olive  = rgb(128,128,  0) */
color: #008000; /* green  = rgb(  0,128,  0) */
color: #008080; /* teal   = rgb(  0,128,128) */
color: #000080; /* navy   = rgb(  0,  0,128) */
color: #C0C0C0; /* silver = rgb(192,192,192) */

There is also a simpler color scale that can be used for many common values, which varies between 0 and 15*15*15 = 3375.

In hexadecimal this range is 0 to #FFF, so here each hexadecimal digit represents red, green, and blue in sequence.

The three-hexadecimal-digit format can be easily converted to the six-hex format by duplicating each digit:

color: #F00; /* red    = #FF0000 = rgb(255,  0,0) */
color: #F60; /* orange = #FF6600 = rgb(255,102,0) */
color: #FF0; /* yellow = #FFFF00 = rgb(255,255,0) */

Question: Which of the other colors above can be expressed in a three-hex format?


Linear Units

Most HTML elements take up some space on your computer screen, so they have at least one and usually two linear dimensions, their width (left to right) and height (top to bottom).

Examples include paragraphs, images, and horizontal rules, as well as borders, font characters, line heights, at al.

CSS can specify linear dimensions with a number of different linear units, each with a two-letter abbreviation that is appended to the numerical value, for example:

border: solid 5px; font-size: 2em; width: 4in;

The Pixel Unit

Graphical user interfaces have long used the dot size of computer displays, the “picture element” or pixel, as a natural unit of linear measurement.

As technology has advanced, the pixel on a typical display has decreased in size, from 1/72 inch on the original Macintosh to now 1/100 inch or less.

So a pixel is a relative unit, varying from display to display.

Nevertheless, it is commonly used in HTML and CSS, abbreviated px:

<p style="font-size: 24px; text-align: center;">Some large text.</p>
<hr style="width: 96px; height: 24px;">

Some large text.


Because printers have a much higher dot density than computer displays, typically 1/600 or 1/1200 inch, any lengths measured in pixels would be very tiny compared to on-screen.

CSS therefore recommends that a pixel be redefined as 1/96 of an inch if it would otherwise be significantly different from this.


Absolute Linear Units

CSS also defines a number of absolute linear units:

Absolute linear units are intended to be the same as in the “real world”.

However, on computer displays they usually aren’t the same, because browsers generally don’t have accurate information about the display depth (i.e. the size of the pixel).

So, for example, the first horizontal rule below should be 1 inch long, but it could be longer or shorter than the second, 96-pixel rule, depending on your display, operating system, browser, and browser preferences:

<hr style="width:  1in;">
<hr style="width: 96px;">


Some browsers make an arbitrary choice, e.g. 1/96 inches per pixel, while others let you set it from a limited range (e.g. 1/72 or 1/96 inches per pixel), and others let you choose a standard length with a slider.

Based on this choice, CSS converts absolute lengths to pixels behind the scenes.

Therefore, it’s adviseable for you to eschew absolute linear units, unless you don’t care about displaying the lengths perfectly accurately (and occasionally wildly wrong).

When printed, absolute lengths will be accurate because printer characteristics are very well defined; so if your main goal is printed output, specifying absolute lengths may also be appropriate.


The Em Unit

It is often very useful to define units based on font characteristics, to provide consistent lengths relative to the text font in use.

All displayed text has a property font-size: (see below), which is approximately equal to the height of a capital letter or the width of the letter ‘m’.

The value of font-size: is therefore called the em, which needs no abbreviation:

<p style="font-size: 24px; border: solid 0.5em; padding: 1em;">Some large text, with a 0.5-em border, followed by a 1-em horizontal rule.</p>
<hr style="width: 24px; margin-left: 66px;">

Some large text, with a 0.5-em border, and a 1-em horizontal rule.



2.4. Exercises

  1. In Dreamweaver’s Code View, edit your class.css, and add some colors to various elements, using keywords, decimal triplets, 6-hex values, and 3-hex values.
  2. Switch to your browser, open/reload your class.html, and verify the result.
  3. Returning to Dreamweaver’s Code View, edit your class.css, and add some font-size: and width: statements to various elements, using px, em, and various absolute units such as in and pc.
  4. Switch to your browser, open/reload your class.html, and verify the result; can you gauge the default font size for your browser?

With CSS, text can be displayed with many different font families, sizes, weights, styles, and colors.


Fonts

You are probably familiar with a large number of font families, such as Times, Helvetica, and Courier.

You have likely also experimented with the font variations within these families, such as size, weight, and style, and also text color.

CSS provides a great deal of control over these possibilities, but they will always depend on one factor beyond your control: which fonts are available on the viewer's computer.

In general these properties are inherited, so they automatically apply to the text inside of contained elements, such as phrases and links:

<p style="font-style: italic;">
    
The following example text comes from
    <a href="http://www.amherst.edu/about_amh/history/">A History of Amherst College</a>.
</p>

    The following example text comes from A History of Amherst College.


The Font Family Property

The same group of text characters can be written in slightly different ways that are nevertheless quite distinct from each other, which are known as font families:

Generic Font Family Sample Text Example Font Families
serif
In 1821 a broad group of local people in and around Amherst worked together to create an institution of higher learning.
Times, New Century Schoolbook, Georgia
sans-serif
In 1821 a broad group of local people in and around Amherst worked together to create an institution of higher learning.
Helvetica, Arial, Verdana
monospace
In 1821 a broad group of local people in and around Amherst worked together to create an institution of higher learning.
Courier, Andale Mono
cursive
In 1821 a broad group of local people in and around Amherst worked together to create an institution of higher learning.
Zapf Chancery, Comic Sans MS
fantasy
In 1821 a broad group of local people in and around Amherst worked together to create an institution of higher learning.
Papyrus

Serif fonts have small embellishments called serifs that make them easier to read on high-resolution technologies such as ink-on-paper; they don't work well at small font sizes on low-resolution devices such as computer screens.

Sans-serif fonts lack serifs, and in printed material are primarily used for headings; on web pages they are also commonly used for body text.

Serif and sans-serif fonts have proportional spacing, meaning their characters have different horizontal widths (compare ‘i’ to ‘w’).

Monospace fonts have the same amount of horizontal space assigned to every character, and are often used to represent typewritten text.

Cursive fonts are intended to simulate handwriting.

Fantasy fonts are other fonts that can't be easily categorized, often suggestive of non-Latin scripts.

(You may also be familiar with "dingbat fonts", which are really distinct characters; you can specify them using character references.)

To specify a font family, use the font-family: property:

<p style="font-family: 'Times New Roman', Georgia, serif;">
    In 1821 a broad group of local people in and around Amherst worked together to create an institution of higher learning.
</p>

In 1821 a broad group of local people in and around Amherst worked together to create an institution of higher learning.

Note that in this example several font families are listed, separated by commas; a web browser will use the first one it can find on the viewer's computer.

Because you can never be certain which font families a browser will find, it's always a good idea to include a generic font family at the end of the list; if it reaches that point, the browser will use its default.

Every browser has some default selection of generic font families, but you can usually specify them yourself in your browser's preferences.

Be aware that font family names with spaces or other special characters in them must be enclosed in single quotes, as for 'Times New Roman' above.


The Font Size Property

As mentioned previously, the size of displayed text, and the relative unit called the em, is given by the font-size: property, which is approximately equal to the height of a capital letter or the width of the letter ‘m’ in the current font family.

Values assigned to font-size: can be any positive length, for example:

<p style="font-family: 'Times New Roman', Georgia, serif; font-size: 20px;">
    Noah Webster, already well known from his textbooks and dictionaries, played a vital role in fundraising and in shaping the institution.
</p>

Noah Webster, already well known from his textbooks and dictionaries, played a vital role in fundraising and in shaping the institution.

You can also use the em to set the value of font-size:, in which case it is redefined to a new length based on its inherited value:

<p style="font-family: 'Times New Roman', Georgia, serif; font-size: 20px;">    <!-- 1 em = 20px -->
    He had served as a
    <span style="font-size: 0.75em;">trustee of Amherst Academy</span>    <!-- 1 em = 15px -->
    since its incorporation in 1815, and was
    <span style="font-size: 150%;">president of its board of trustees</span>    <!-- 1 em = 30px -->
    during the critical 1820-21 period, when Amherst College was formed.
</p>

He had served as a trustee of Amherst Academy since its incorporation in 1815, and was president of its board of trustees during the critical 1820-21 period, when Amherst College was formed.

Note that for the font-size: property, 100% = 1 em.

Every browser has some default value for font-size: (at this writing commonly 16px), but you can usually change that in your browser's preferences.

Quandary: if you assign a font-size: in pixels, users of the Windows version of Internet Explorer can't change the size on the fly with their browser’s menu item View > Text Size; the alternative is to make all font measurements relative to the default font-size: (with the em or %), but that can be noticeably different between browsers!


The Font Weight Property

The property font-weight: has two absolute values available, the keywords normal (the default) and bold:

<p style="font-weight: bold;">
    Webster had been on the committee formed in 1818 by the Academy's trustees
    to call a regional convention regarding founding
    <span style="font-weight: normal;">a new educational institution</span>
    — that which eventually materialized as Amherst College.
</p>

Webster had been on the committee formed in 1818 by the Academy's trustees to call a regional convention regarding founding a new educational institution — that which eventually materialized as Amherst College.

There are additional values defined for font-weight:, but their support is font-dependent and inconsistent across browsers, so they won't be discussed here.


The Font Style Property

While "font style" has a more general meaning, in CSS the property font-style: lets you select between the values of normal (upright), italic, or oblique (slanted) text:

<p style="font-style: normal;">
    In addition to educating the indigent,
    <span style="font-style: italic;">the new college</span>
    
showed awareness of and early support for others
    who might not commonly have had access to
    <span style="font-style: oblique;">higher education</span>.
</p>

In addition to educating the indigent, the new college showed awareness of and early support for others who might not commonly have had access to higher education.

An italic font is generally designed to be more than simply slanted text, but at this time most browsers seem to treat it as interchangeable with oblique.


The Font Variant Property

Currently CSS defines two possible values for the property font-variant:, the keywords normal (the default) and small-caps:

<script>
    .proper-noun { font-variant: small-caps; }
</script>
<p>

    The college's first African-American graduate,
    <span class="proper-noun">Edward Jones</span>,
    was a member of the Class of 1826; he eventually settled in
    <span class="proper-noun">Sierra Leone</span>
    and became principal of the
    <span class="proper-noun">Fourah Bay Christian Institution</span>
    (forerunner of
    <span class="proper-noun">Fourah Bay College</span>).
</p>

The college's first African-American graduate, Edward Jones, was a member of the Class of 1826; he eventually settled in Sierra Leone and became principal of the Fourah Bay Christian Institution (forerunner of Fourah Bay College).


The Font Property

Quite often you will need to use more than one of the above properties, font-family:, font-size:, font-weight:, font-style:, or font-variant:, in a single style, and it can quickly become tedious writing them out.

Fortunately, the values of these properties have been designed so that they do not overlap, so you can use them with the single short-hand property font::

<p style="font: bold italic small-caps 0.8em 'New Century Schoolbook', serif;">
    Amherst's first Japanese graduate, the young samurai Joseph Hardy Neesima,
    Class of 1870, fled Japan when foreign travel was still prohibited.
    Neesima converted to Christianity and returned to Japan in 1875
    to found the school that would become Doshisha University,
    Amherst's sister institution in Kyoto.
</p>

Amherst's first Japanese graduate, the young samurai Joseph Hardy Neesima, Class of 1870, fled Japan when foreign travel was still prohibited. Neesima converted to Christianity and returned to Japan in 1875 to found the school that would become Doshisha University, Amherst's sister institution in Kyoto.

The only restriction here is that font-size: and font-family: must be present at the end of the style declaration, and in that order.

Also be aware that if you leave out values for one or more of the other three properties, they will be set to their default values of normal.


The Color Property

The color of text is given by the color: property, using one of the color values described previously:

<p style="color: blue;">
    
Amherst College first admitted women in 1975.
</p>

Amherst College first admitted women in 1975.

Note: the color: property also applies, by default, to element borders, which will be discussed later.


The Text Decoration Property

In addition to the common underline, CSS provides an expanded set of text embellishments using the property text-decoration:

<p>
    <span style="text-decoration: underline;">Today the diverse and international student body includes men and women</span>
    <span style="text-decoration: overline;">from a wide variety of ethnic, cultural and socio-economic backgrounds</span>
    <span style="color: red; text-decoration: line-through;">from all over the United States and more than 40 other countries</span>.
</p>

Today the diverse and international student body includes men and women from a wide variety of ethnic, cultural and socio-economic backgrounds from all over the United States and more than 40 other countries.

The default value of text-decoration: is none.

Note that the value of the color: property applies both to text and these decorations.

The text-decoration: property is not automatically inherited, however the embellishments will extend across any child elements, which has a similar effect, as in the blue phrase below:

<p style="text-decoration: underline;">
    Today the diverse and international student body includes men and women
    <span style="color: blue;">from a wide variety of ethnic, cultural and socio-economic backgrounds</span>
    <span style="color: red; text-decoration: underline;">from all over the United States and more than 40 other countries</span>.
</p>

Today the diverse and international student body includes men and women from a wide variety of ethnic, cultural and socio-economic backgrounds from all over the United States and more than 40 other countries.

However, if you want the embellishment to match the color of the child element's text, you will also need to apply text-decoration: to the child, as in the red phrase above.


2.5. Exercises

  1. In Dreamweaver’s Code View, edit your class.css, and try out some different font families, font sizes, font weights, font styles, and font variants.
  2. Switch to your browser, open/reload your class.html, and verify the result.
  3. Returning to Dreamweaver’s Code View, edit your class.css, and put these different properties together in a single font: property.
  4. Switch to your browser, open/reload your class.html, and verify the result; do you see any difference?
  5. Returning to Dreamweaver’s Code View, edit your class.css, and try out the color: and text-decoration: properties.
  6. Switch to your browser, open/reload your class.html, and verify the result.

Most HTML elements fill some area, and can be surrounded by padding.


The Content Area

Every HTML element with visible content, such as text-filled paragraphs, images, and horizontal rules, places that content into a rectangular region called its content area.

The content area, shown in black below, will generally be at least large enough to hold the element’s content:

1820: Cornerstone laid for South College, first College building.

The horizontal and vertical dimensions of the content area are given by two properties with linear values, width: and height::

<hr style="width: 1in; height: 4px;">
<div style="width: 50%; height: 3em;">
    
1821: The College is founded as Amherst Collegiate Institution.
</div>

1821: The College is founded as Amherst Collegiate Institution.

Percentage values are calculated from the width of the containing block (the rose area in this last example).

The properties width: and height: must never be negative, and are not automatically inherited.


Automatic Widths and Heights

If the width: and height: of the content area aren’t specified, the default value for both is auto, which means they are calculated automatically, based on the nature of the element and its environment.

The width and height of in-line nonreplaced elements, e.g. <span> and <em>, cannot be changed from their automatic values (to be discussed later).

The width and height of in-line replaced elements such as images have auto values equal to their actual size in pixels.

Horizontal rules have auto heights that are typically one or two pixels, and auto widths of 100% (i.e. they extend all the way across the browser window or their parent element, if smaller).

Many block container elements, such as <div> and <p>, will calculate the minimum necessary height based on the text, images, and other elements they contain, but they will have an auto width of 100% (even if their contents don’t actually fill that space):

<div>
    
1821: Zephaniah Swift Moore, clergyman and former president of Williams College, begins term as first president (1821-23).
</div>
1821: Zephaniah Swift Moore, clergyman and former president of Williams College, begins term as first president (1821-23).

Tables, on the other hand, will calculate both the minimum necessary height and width for their content:

<table><tr><td>
    
1822: First College Catalog issued-a single sheet.
</td></tr></table>
1822: First College Catalog issued-a single sheet.

Backgrounds

The color of the content area, behind the content itself, is given by the background-color: property:

<div style="background-color: rgb(133,127,11);">
    
1823: Clergyman Heman Humphrey begins term as second president (1823-45).
</div>
1823: Clergyman Heman Humphrey begins term as second president (1823-45)

The background can also be filled by an image, for example a pattern like , by specifying its location with a URL:

<div style="background-image: url(pattern.png);">
    
1825: Massachusetts Legislature grants charter to create Amherst College.
</div>
1825: Massachusetts Legislature grants charter to create Amherst College.

Warning: if you specify the location of an image with a relative URL such as the above, it is relative to the style’s location, which could be the html document or an external style sheet.

Because the name space for the values of these properties is designed to not overlap, you can also use the short-hand property background: to specify either or both of them:

<div style="background: url(pattern.png) rgb(133,127,11);">
    
1825: First graduating class (25 seniors) receives degrees.
</div>
1825: First graduating class (25 seniors) receives degrees.

Providing a back-up color specification like this is a good idea in case there is a problem loading the image (the image will always be laid on top of the color, independent of their order).

The default value of background-color: is transparent and background-image: is none.

Neither of these properties is inherited.

Question: If background: isn't inherited, why do we see it in the <dfn> element in the following?

1825: College adopts corporate seal and motto. The seal shows a sun and a Bible illuminating a globe with the motto Terras Irradient (“Let them enlighten the lands”) underneath.

Element Padding

It’s often useful to offset content from its surroundings, and in CSS there are two ways to do this, padding and margins (the latter to be discussed later).

The simplest way to offset content is with blank space that immediately surrounds called padding.

CSS allows any element to have padding, which is filled with the element’s background: color (black in the following, outside of the white dotted line bounding the content area):

1827: Johnson Chapel dedicated.

The content area together with the padding increases the size of the element so that it fills a larger element box (the rectangular region inside the dashed border above).


Padding Sizes

The width of padding can be controlled by four properties:

  padding-top:  
padding-left: 1827: Johnson Chapel dedicated. padding-right:
  padding-bottom:  

For example:

<div style="background: black; color: white; padding-top: 10px; padding-right: 20px; padding-bottom: 30px; padding-left: 40px;">
    
1828: The students organize a form of government for ensuring more order on campus.
    As the &ldquo;House of Students,&rdquo; they make laws for the protection of the buildings and grounds,
    and promote better observance of study hours.
</div>
1828: The students organize a form of government for ensuring more order on campus. As the “House of Students,” they make laws for the protection of the buildings and grounds, and promote better observance of study hours.

With content and padding, the space used by the element box is:

Percentage values are calculated from the width of the containing block.

The four padding values by default are zero, must never be negative, and are not automatically inherited.


The Padding Property

The padding: property provides a convenient shorthand for setting all four of the individual paddings, always starting with padding-top: and proceeding in a clockwise direction:

So, the previous example could been written more simply as:

<div style="background: black; color: white; padding: 10px 20px 30px 40px;">
    1828: The students organize a form of government for ensuring more order on campus.
    As the &ldquo;House of Students,&rdquo; they make laws for the protection of the buildings and grounds,
    and promote better observance of study hours.
</div>

Another example shows how some margins are set automatically:

<div style="background: black; color: white; padding: 2em 1em;">
    1835: Professor of Mathematics Ebenezer S. Snell,
    one of the first two College graduates (AC 1822),
    begins to systematically keep weather records.
</div>
1835: Professor of Mathematics Ebenezer S. Snell, one of the first two College graduates (AC 1822), begins to systematically keep weather records.

2.6. Exercises

  1. In Dreamweaver’s Code View, edit your class.css, and a background color to one block and one in-line element, and a background image to one other block element.
  2. Switch to your browser, open/reload your class.html, and verify the result.
  3. Returning to Dreamweaver’s Code View, edit your class.css, and add some padding to different elements, both block and in-line; try out both the padding-side: properties as well as the padding: property.
  4. Switch to your browser, open/reload your class.html, and verify the result.

Most HTML elements can be given a border, and CSS provides many options.


Element Borders

CSS lets almost any element have a border, a usually narrow visible boundary that lies just outside the padding or, if there is no padding, just outside the content area.

In the following two representations, the content area and padding (in the second case) are black, and the border is solid white:

1827: Johnson Chapel dedicated.

1827: Johnson Chapel dedicated.

The element box is, again, indicated by the dashed border.

The presence of borders is determined by the property border-style:, which by default is set to none.

The other possible values of border-style: are:

solid
double
dotted
dashed
groove
ridge
inset
outset

Like padding, borders are not automatically inherited.


Individual Border Styles

Border styles can be set individually for each side of the content area by using the four specific properties:

  border-top-style:  
border-left-style: 1827: Johnson Chapel dedicated. border-right-style:
  border-bottom-style:  

For example:

<div style="border-top-style: double; border-right-style: solid; border-bottom-style: dashed; border-left-style: dotted;">
    
1836: A disagreement about appointments to the Junior Exhibition
    sparks the Gorham Rebellion, led by William O. Gorham (AC 1838).
    In response to the protest, the College threatens to remove the Class of 1838.
    The students backed down, but the controversy was the subject of discussion for years.
</div>

1836: A disagreement about appointments to the Junior Exhibition sparks the Gorham Rebellion, led by William O. Gorham (AC 1838). In response to the protest, the College threatens to remove the Class of 1838. The students backed down, but the controversy was the subject of discussion for years.

Border styles can also be set collectively with border-style: top right bottom left and up to four values, in the same manner as for padding::

<div style="border-style: double dotted;">
    
1837: Amherst's first fraternity, Alpha Delta Phi, is established.
</div>
1837: Amherst's first fraternity, Alpha Delta Phi, is established.

Border Colors

By default a border’s color is the same as for the element’s text, which is set by the property color (see below).

Border colors can be set individually for each side of the content area by using the four specific properties:

  border-top-color:  
border-left-color: 1827: Johnson Chapel dedicated. border-right-color:
  border-bottom-color:  

For example:

<div style="border-style: solid; border-top-color: red; border-right-color: yellow; border-bottom-color: green; border-left-color: blue;">
    1842: Amherst's first Alumni Association is formed at reunion, July 27.
</div>
1842: Amherst's first Alumni Association is formed at reunion, July 27.

The border colors can also be set collectively with border-color: top right bottom left and up to four values, in the same manner as for padding: and border-style::

<div style="border-style: dashed; border-color: orange;">
    
1844: A gift of $10,000 from Boston merchant David Sears
    rescues Amherst College during a difficult time and
    marks the beginning of the Sears Foundation of Literature and Benevolence.
</div>
1844: A gift of $10,000 from Boston merchant David Sears rescues Amherst College during a difficult time and marks the beginning of the Sears Foundation of Literature and Benevolence.

Note that the content area’s background: color extends under the border, so that those with space in them (double, dotted, dashed) reveal that color.


Border Widths

The width of borders can be controlled individually for each side of the content area by using the four specific properties:

  border-top-width:  
border-left-width: 1827: Johnson Chapel dedicated. border-right-width:
  border-bottom-width:  

For example:

<div style="border-style: solid; border-top-width: 2em; border-right-width: 1.5em; border-bottom-width: 1em; border-left-width: 0.5em;">
1845: Edward Hitchcock, geologist and clergyman who had taught at Amherst since 1825, was the College&rsquo;s third president (1845-54).
</div>
1845: Edward Hitchcock, geologist and clergyman who had taught at Amherst since 1825, was the College’s third president (1845-54).

Border widths can also be set collectively with the short-hand property border-width: top right bottom left in the same way as for padding:, border-style:, and border-color::

<div style="border-style: solid; border-width: 0.5em 1em 0;">
1853: Morgan Library completed. It has 296 donors and the surplus funds are used for books. Henry Wadsworth Longfellow is among the donors.
</div>
1853: Morgan Library completed. It has 296 donors and the surplus funds are used for books. Henry Wadsworth Longfellow is among the donors.

With content, padding, and borders, the dimensions of the element box are given by:

Percentage values are calculated from the width of the containing block; negative values are not permitted.

Note that if a border or border side has a border-style: of none, its width will always be taken to be zero.


Border-Side Properties

Sometimes you want to set the style, color, and width properties for just one border; thankfully there is a shorthand for each side:

  border-top:  
border-left: 1827: Johnson Chapel dedicated. border-right:
  border-bottom:  

These properties can take any of the values associated with the three corresponding properties above, border-side-style:, border-side-color:, and border-side-width:, in any order:

<div style="border-bottom: solid green 0.5em;">
1854: William Augustus Stearns, clergyman and biblical scholar, begins term as fourth president (1854-76).
</div>
1854: William Augustus Stearns, clergyman and biblical scholar, begins term as fourth president (1854-76).

The Border Property

Finally, the border: property provides a convenient shorthand for setting all three of the collective properties above, border-style:, border-color:, and border-width:, but only if you want uniform borders:

<div style="border: solid green 0.5em;">
1855: The Olio begins in October 1855. Initially in newspaper form and known as the College Olio, the first versions of the publication are more a combination of college catalog and student newspaper than yearbook.
</div>
1855: The Olio begins in October 1855. Initially in newspaper form and known as the College Olio, the first versions of the publication are more a combination of college catalog and student newspaper than yearbook.

If you want to modify one or more sides to have slightly different properties, you can list them following border: and they will take precedence:

<div style="border: solid green 0.5em; border-bottom-style: dashed;">
1857: Massachusetts Governor Joel Hayden of Haydenville gives the College Sabrina, a statue of a wood nymph. For years, the statue is the subject of class rivalries in which one class would steal and hide the statue from another.
</div>
1857: Massachusetts Governor Joel Hayden of Haydenville gives the College Sabrina, a statue of a wood nymph. For years, the statue is the subject of class rivalries in which one class would steal and hide the statue from another.

2.7. Exercises

  1. In Dreamweaver’s Code View, edit your class.css, and add borders of different styles, colors, and widths to various elements, both block and in-line; try out the individual properties as well as the border-side: properties and the border: property.
  2. Switch to your browser, open/reload your class.html, and verify the result.

Many HTML elements automatically have margins, and CSS lets you control them.


Element Margins

As previously mentioned, CSS provides two ways to offset content from its surroundings, padding and margins.

CSS allows almost any element to have a margin, a blank space that separates its border from other elements.

In the representation below, the margin lies outside of the element's content area (black with text), padding (black), and border (white), and is outlined with a dashed line.

1827: Johnson Chapel dedicated.

Margins are always transparent, so that any colors behind the element will show through (rose in this case).

There are a number of characteristics that distinguish margins from padding:

  1. exterior position relative to an element border;
  2. transparent always, rather than possibly being filled with a background;
  3. values that can be negative;
  4. default values that may be non-zero.

The last two will be discussed in more detail shortly.


Margin Sizes

The width of margins can be controlled by four properties:

  margin-top:  
margin-left: 1827: Johnson Chapel dedicated. margin-right:
  margin-bottom:  

For example:

<div style="margin-top: 10px; margin-right: 20px; margin-bottom: 30px; margin-left: 40px;">
1859: In July in Pittsfield, Massachusetts, Amherst and Williams play the first intercollegiate baseball game. After 3 1/2 hours and 26 innings, Amherst defeats Williams, 73-32.
</div>
1859: In July in Pittsfield, Massachusetts, Amherst and Williams play the first intercollegiate baseball game. After 3 1/2 hours and 26 innings, Amherst defeats Williams, 73-32.

Percentage values are calculated from the width of the containing block.

With content, padding, borders, and margins, the final dimensions of the element box are:

Margins can have negative values; effectively this can push borders or content outside of the element box.

The margin properties are not automatically inherited.


The Margin Property

Margins can also be set collectively with the short-hand property margin: top right bottom left, in the same way as for padding: and border-width:, et al.:

<div style="margin: 1% 2% 3% 4%;">
1860: Barrett Gymnasium is constructed, housing the Department of Hygiene and Physical Education. The department was established in 1860 in an attempt to combat student ill health, making Amherst the first college to introduce physical education and hygiene into the curriculum.
</div>
1860: Barrett Gymnasium is constructed, housing the Department of Hygiene and Physical Education. The department was established in 1860 in an attempt to combat student ill health, making Amherst the first college to introduce physical education and hygiene into the curriculum.

Vertical Margins

As discussed previously, an element's content area will have a certain size based on its content and on the nature of the element and its environment.

A number of HTML elements also set default margins in the vertical direction that are different from zero.

For example, the paragraph <p> and header elements usually have margin-bottom: set to 1em , and some browsers do the same to margin-top::

<p>1866: College adopts school colors of mauve and white, later changed to purple and white.</p>
<p style="margin-top: 2em; margin-bottom: 0.5em;">1868: The Amherst Student publishes its first issue on February 1. Its focus is on "topics immediately associated with college life, and a summary of college news," rather than on the literary and philosophical essays and articles of previous College publications.</p>
<p>
Walker Hall cornerstone laid June 10. The building was completed in 1870.</p>

1866: College adopts school colors of mauve and white, later changed to purple and white.

1868: The Amherst Student publishes its first issue on February 1. Its focus is on "topics immediately associated with college life, and a summary of college news," rather than on the literary and philosophical essays and articles of previous College publications.

Walker Hall cornerstone laid June 10. The building was completed in 1870.

You can see here an interesting characteristic of vertical margins: they are allowed to collapse, i.e. they overlap as much as possible with extending into borders/padding/content, effectively eliminating the smaller of the two.


Horizontal Margins

Generally speaking, the properties margin-left: and margin-right: have default values of zero, but certain elements may set them to something different for their circumstances.

For example, if a horizontal rule has a width: less than 100%, then margin-left: and margin-right:will be set to auto, which means they will be equal and as large as necessary to keep the rule centered on the page:

<hr style="width: 50%;">


2.8. Exercises

  1. In Dreamweaver’s Code View, edit your class.css, and add margins of different sizes to various elements, both block and in-line; try out both the the margin-side: properties and the margin: property.
  2. Switch to your browser, open/reload your class.html, and verify the result.

Content positioning refers to alignment, indentation, spacing, and other characteristics of a block of text and images.


Content Alignment

Because the content in the content area doesn’t always fill it, the content can be positioned within that area with the property text-align: (whether or not it’s actually text):

<h3 style="text-align: left;">From <a href="http://www.amherst.edu/about_amh/history/">A History of Amherst College</a>:</h3>
<p style="text-align: center;">
In 1821 a broad group of local people in and around Amherst worked together to create an institution of higher learning for the education of indigent young men of piety and talents for the Christian ministry (as the call for donations to the initial endowment, the Charity Fund, phrased it).</p>
<p style="text-align: right;">
Noah Webster, already well known from his textbooks and dictionaries, played a vital role in fundraising and in shaping the institution. He had served as a trustee of Amherst Academy since its incorporation in 1815, and was president of its board of trustees during the critical 1820-21 period, when Amherst College was formed.</p>
<p style="text-align: justify;">
Webster had been on the committee formed in 1818 by the Academy&rsquo;s trustees to call a regional convention regarding founding a new educational institution &mdash that which eventually materialized as Amherst College.</p>

From A History of Amherst College:

In 1821 a broad group of local people in and around Amherst worked together to create an institution of higher learning for the education of indigent young men of piety and talents for the Christian ministry (as the call for donations to the initial endowment, the Charity Fund, phrased it).

Noah Webster, already well known from his textbooks and dictionaries, played a vital role in fundraising and in shaping the institution. He had served as a trustee of Amherst Academy since its incorporation in 1815, and was president of its board of trustees during the critical 1820-21 period, when Amherst College was formed.

Webster had been on the committee formed in 1818 by the Academy’s trustees to call a regional convention regarding founding a new educational institution — that which eventually materialized as Amherst College.

In western encodings like ISO-8859-1 (Latin-1), the default value of text-align: is left.

text-align: is an inherited property.

Element positioning is a powerful way to organize a document.


Element Positioning

The primary selectors can be combined with each other to create a richer set of selectors.


The Class-Identifier Selector

An HTML element can have both class and identifier attributes, and CSS can select for the combined appearance of these attributes’ values.

The syntax is to list the selectors sequentially, without a space (the order is unimportant):

<style type="text/css">
    p { font-style: normal; text-align: left; }
    .urgent { font-weight: bold; border: dotted blue 5px; background: orange; }
    .urgent#lastnotice { color: red; }
</style>
....
<p class="urgent">
Urgent!</p>
<p id="lastnotice" class="urgent">
This is your last chance!</p>

Urgent!

This is your last chance!

Note that the <p id="lastnotice" class="urgent"> tag matches all three of the p, .urgent, and .urgent#lastnotice selectors.

Question: Does it make sense to define a separate #lastnotice selector?


The Element-Identifier Selector

It’s possible to restrict element selectors so that they only apply in the presence of a particular identifier.

The syntax, again, is to list the selectors sequentially, without a space:

<style type="text/css">
    p { font-style: normal; text-align: left; }
    p#important { color: red; border: solid; }
</style>
....
<p>
This paragraph will not have any special formatting.</p>
<p id="important">
But this important paragraph will have red text and border.</p>

This paragraph will not have any special formatting.

But this important paragraph will have red text and border.

Note that the <p id="important"> is matched by both the p and p#important selectors.


The Element-Class Selector

It’s also possible to restrict element selectors so that they only apply in the presence of a particular class selector.

The syntax, again, is to list the selectors sequentially, without a space:

<style type="text/css">
    p { font-style: normal; text-align: left; }
    .urgent { font-weight: bold;  border: dotted blue 5pt; background: orange; }
    p.urgent { font-size: 120%; }
</style>
....
<p>
This paragraph will not have any special formatting.</p>
<ul><li class="urgent">
This urgent list item will be bold and have a dotted blue boundary.</li></ul>
<p class="urgent">This urgent paragraph will be the same, but in a larger size.</p>

This paragraph will not have any special formatting.

This urgent paragraph will be the same, but in a larger size.

Note that the <p class="urgent"> tag matches all three of the p, .urgent, and p.urgent selectors.


The Pseudo-Class Selector

The identifier and class selectors are based on the value of HTML attributes, which are usually fixed in an element (ex; pseudo-class selectors are based on the state of an HTML element, which can change as a user surfs the net.

For example, you have certainly noticed that hypertext links, defined by the anchor element, will commonly change color depending on whether the sites they point to have been visited or not.

A pseudo-class is introduced by the colon (:), and the possible states and their meanings are:

Pseudo-Class Required Element Selector Meaning
:link a The element has an href attribute that has not yet been visited.
:visited a The element has an href attribute that has been visited.
:focus The element has the input focus, e.g. it can accept keyboard entry.
:hover The element is underneath the cursor, but the mouse is not depressed.
:active The element is underneath the cursor and the mouse is depressed.

Although the last three pseudo-classes can apply to any element, at this time they only function consistently across browsers with the anchor element; it doesn’t hurt to use them, though.

As with other element-selectors, the syntax is to list the selectors sequentially, without a space:

<style type="text/css">
    p { font-style: normal; text-align: left; }
    a:link { color: #2659BF; text-decoration: underline; }
    a:visited { color: #666666; text-decoration: underline; }
    a:hover { color: #0000FF; }
    a:active { color: #9999FF; }
    :focus { background: #9999FF; }
    .standout:hover { font-size: 120%; }
</style>
....
<p>
This plain paragraph has an <a href="http://www.amherst.edu">important link</a> in it,
and this text field will be <input type="text" size="11" value="highlighted"> when you click on it.</p>
<p class="standout">
This urgent paragraph may do something interesting on hovering.</p>

This plain paragraph has an important link in it, and this text field will be when you click on it.

This urgent paragraph may do something interesting on hovering.

Due to limitations in some browsers, it’s best to only apply one pseudo-class selector at a time.

It’s also a good idea to order them as in the table above; Dreamweaver can sometimes put them in a different order, so beware.


The Descendant Selector

Element selectors (as well as element-identifier and element-class selectors) can be restricted so that they only apply to descendant elements.

The syntax to describe a descendant selector is to list the ancestor and descendant sequentially, separated by a space:

<style type="text/css">
    p { font-style: normal; text-align: left; }
    strong { font-weight: bold; }
    p strong { background: red; }
</style>
....
<p>
This <strong>important phrase</strong> will have a red background.</p>
<p>
This <code><strong>important piece of code</strong></code> will also have a red background.</p>
<ul>
<li>But in this list item a <strong>similar phrase</strong> will not.
</ul>

This important phrase will have a red background.

This important piece of code will also have a red background.

Note that the text inside the <p>...<strong> element structure matches both the p, strong, and p strong selectors.

The second paragraph could also be matched by the selector p code strong, but it wouldn’t apply to the first paragraph.


2.11. Exercises

  1. In Dreamweaver’s Code View, edit your class.css, and add examples of .class#identifier, .class.class, element#identifier, and element.class rules, as well as HTML tags to which they apply.
  2. Switch to your browser and open/reload the file to see the effect of these styles.
  3. Again in Dreamweaver’s Code View, edit your class.css, and add examples of pseudo-class selector rules, as well as HTML tags to which they apply.
  4. Switch to your browser and open/reload the file to see the effect of these styles.
  5. Returning to Dreamweaver’s Code View, edit your class.css, and add examples of descendant selector rules, as well as HTML tags to which they apply.
  6. Switch to your browser and open/reload the file to see the effect of these styles.