Professional Documents
Culture Documents
Introduction
As you have seen in the materials presented so far, a cascading style sheet (CSS)
provides a convenient way to specify the styles for a web page. Cascading style sheets
are composed of rules. A rule consists of a selector and one or more declarations that
can specify the size and color of text, background colors, fonts, how links work, how an
item on the page behaves when the mouse hovers over it, and so forth. A selector is
usually an HTML tag such as p or body. Let’s look at an example of a CSS rule:
This would make the font of the entire body of the HTML document Arial and also bold.
Notice how the declarations are contained in braces and separated by a semicolon.
You may not want to have a declaration apply to all tags in a document of a certain
type. For example, perhaps you only want certain types of anchor tags to change color
after being clicked. In this case, you can have a special class that you add to the HTML
tag and then reference this in the CSS rule. Let’s look at an example. First, here is a
snippet of HTML code:
The rule shown above will cause all anchors of class linkType1 to be underlined. In the
HTML code above, only Google will be underlined. Amazon will not be.
In addition to specifying a class for a specific type of tag, you can also specify a rule for
a class for all types of tags that are named with the class. Let’s look at some HTML:
.blueText {color:blue;}
The first line, which is part of a paragraph tag, will appear in blue. The second line will
be black. The table will contain a blue cell and a black cell. So, you can see that the
rule applies to any type of HTML tag as long as it has a class of “blueText.”
Style sheets are cascading, since a property set in a child tag will override a property
set by a parent. While it is beyond the scope of this laboratory to review every possible
thing that one can do with cascading style sheets, let’s discuss a couple of types of style
sheets and how these work.
Internal cascading style sheets are contained within the head of an HTML document.
The advantage to this is that all of your styles are easily seen when writing HTML tags.
The big disadvantage is that you cannot easily reuse the cascading style sheet code,
since you will have to copy it into other HTML documents. An example of an internal
CSS is shown in Figure 1 below.
Figure 1
Let’s discuss this: Lines 6 and 7 are simply part of the HTML document and specify that
we are currently working in the head, and they give the head a title. Line 8 lets the
browser know that inside of the style tag is a set of styles that are of type CSS. Lines 9
through 16 contain the actual CSS code. As mentioned above, the CSS rule has a
format with a selector and one or more declarations. Line 9 specifies that the body
Saylor URL: www.saylor.org/CS412 subunit 3.2 (end)
Figure 2 shows the use of the internal CSS within an HTML document. Download the
html code here and try to render this in a browser to see how it works.
Review Question:
1. How would the page render if the selector for line 11 was just a and not a.link1?
In the example shown above, we used an internal style sheet. While this is convenient,
it does not lend itself to reuse. Also, anyone can view the source of an HTML page and
see your styles. A better approach is to use an external style sheet, which is simply a
file with a css extension. Figure 3 shows the CSS code from the example above,
rewritten as an external style sheet.
Rather than present the entire HTML document again, Figure 4 shows only the part that
was changed to use the external style sheet.
<head>
<title>External Style Sheet Example</title>
<link rel = "stylesheet" type = "text/css" href="example1.css" />
</head>
Figure 4
Lab Assignment
Task 1
Given the HTML code in Figure 5 (also found here), add CSS code to accomplish the
following:
1. Change the font-family for the body to arial, helvetica, and sans-serif.
7. Changed the background to light gray (#D8D8D8) for ordered list elements (li tag)
with a shaded class.
8. Change any text within a paragraph tag with a note class to centered, color red, and
underlined (hint: use text-decoration).
You should make this an external style sheet so that you can use it on other HTML files.
When this renders after the CSS has been written, it should look like Figure 6.
Figure 6
Saylor URL: www.saylor.org/CS412 subunit 3.2 (end)
Take a look at the HTML code in Figure 7(also found here). Add a call to your external
CSS file to the code and render this in a browser.