CSS (Cascading Style Sheets)CSS (Cascading Style Sheets) are a well-established part of web design, and they find
frequent use in classic web applications as well as in Ajax. A stylesheet offers a
centralized way of defining categories of visual styles, which can then be applied
to individual elements on a page very concisely. In addition to the obvious styling
elements such as color, borders, background images, transparency, and size,
stylesheets can define the way that elements are laid out relative to one another
and simple user interactivity, allowing quite powerful visual effects to be achieved
through stylesheets alone.
In a classic web application, stylesheets provide a useful way of defining a style
in a single place that can be reused across many web pages. With Ajax, we don’t
think in terms of a rapid succession of pages anymore, but stylesheets still provide
a helpful repository of predefined looks that can be applied to elements dynami-
cally with a minimum of code. We’ll work through a few basic CSS examples in this
section, but first, let’s look at how CSS rules are defined.
CSS styles a document by defining rules, usually in a separate file that is
referred to by the web page being styled. Style rules can also be defined inside a
web page, but this is generally considered bad practice.
A style rule consists of two parts: the selector and the style declaration. The selec-
tor specifies which elements are going to be styled, and the style declaration
declares which style properties are going to be applied. Let’s say that we want to
make all our level-1 headings in a document (that is, the