Chapter 7: Stylesheet Structure
A style sheet contains one or more styles, usually called rules. Each rule
has the following structure:
selector {formatting}
- selector specifies which parts of the HTML document the rule is to be applied to.
- formatting defines the formatting for the rule.
Most of the remainder of this chapter is devoted to showing you how to
create selectors and formatting rules.
A style sheet can be located either within the
HTML document, in the
head section, or as a separate file that is linked to the HTML document. In
fact a given HTML document can have both internal and external style
sheets, one of the
CSS features that makes it so flexible.
What Does "Cascading" Mean?
The term "cascading" in Cascading Style Sheets reflects two aspects of
style sheets work. The first is the way style sheets interact. An
HTML
document can have more than one style sheet associated with it, for
example one internal and one external. Style rules cascade so that the end
result is a combination of the document's various style sheets.
The aspect of cascading is the operation of inheritance. If a certain aspect
of formatting is not specified for a child element, it will (in most cases)
inherit the formatting of its parent (enclosing) element. For example, a
<p> element that has no font style rule will display in the font specified
for its parent, the <body> element. The formatting "cascades" down
the chain from parent to child.
To create an internal style sheet, place the following in the head section of
the
HTML document:
<style>
<!--
....style rules go here....
//-->
</style>
To use an external style sheet, use a text editor to create a text file
containing the style rules and save it with the .css extension. Then place a
reference to the file in the head section of the
HTML document as
follows:
<link rel="stylesheet" type="text/css"
href="MyStyleSheet.css" />
This assumes that the style sheet file is in the same folder on the Web
server as the Web page. If not, you must specify the full path in the
href
attribute.
A style sheet can also contain comments. They are ignored in processing
and can be useful for documenting your styles. A comment starts with the
characters /* and ends with */. For example:
/* This is a CSS comment. */
/* Comments can be spread over
two or more lines
line this one. */