When HTML was first introduced, there were a number of tags whose purpose was just for formatting: for example, tags that center or switch to an italic font. With HTML version 4.0, such tags were made obsolete (though most browsers still support them), in favor of a formatting technique called Cascading Style Sheets.
When a browser is displaying a page, there is a step of reading a
tag, such as EM or H1 and then determining
what the results are to look like. (This is sometimes call
rendering a page.) Each browser has a default way of
rendering a tag. For example, most browsers will render the
EM tag by switching to an italic version of the current
font. Most browsers will render an H1 tag by switching
to a very large version of the current font. Several important points
to make about this:
BLOCKQUOTE in Safari
indents by a different amount than IE does.
EM tag to look red, you can do that. The style of tags
is determined by something called CSS, for Cascading Style
Sheets.
You can specify the style of a tag using a style sheet. These style sheets can be put in (at least) three different places:
HEAD of a document, using a special tag
LINK tag. You saw an example of that on the first
day, and if you view the source for this web page, you'll see
another example.
Suppose that we always want EM to be red. After all, EM stands for
emphasis, and what could be better emphasis than putting something in red?
(FYI, using color is, in fact, a terrible idea, because some
people are color-blind or even completely blind. We'll talk more about
the issue of accessibility in a later lecture. For now, for the purposes
of this lecture's examples, let's suppose that using red is a sensible
thing to do.) Let's look at the three ways to make EM red:
STYLE
attribute of the tag to set the COLOR
property to have the value red and the
BACKGROUND property to have the value white.
(You should be reminded of the attribute/value idea in tags, but
the syntax is different. With CSS, attribute/value pairs are
separated by semi-colons, and the attribute ends with a colon.)
Note that you should always pick both colors, in
case the user's browser has specified a background color that
matches your foreground color. Here's the example:
this is an <EM STYLE="color: red; background: white">important</EM> wordresults in
this is an important word
STYLE tag in the HEAD of your
document to specify the style for the EM tag once and
for all:
<style type="text/css">
EM {
color: red;
background: white
}
</style>
Later in your document, you just use the EM tag
normally, and it's rendered as red.
STYLE tag. Instead, you
reference the file using the LINK tag in the
HEAD, the same place you would use the
STYLE tag. Suppose we create a file called
red-em.css that contains the following:
EM
{
color: red;
background: white
}
and we reference it in the HEAD thus:
<LINK REL="stylesheet" TYPE="text/css" HREF="red-em.css">
As with document-level style sheets, we can just use the EM tag
normally from then on.
There are several facets to the syntax of styles:
color) are followed
by a colon, then at least one space, then the value of the
property (such as red). The only properties we've
learned so far are color and background
(which is a shorthand for background-color), but
we'll learn more soon. The discussion of fonts will cover font
properties such as weight and size, for
example. If you want to specify more than one property/value
pair, separate them with semi-colons. The order of the style
properties does not matter.
STYLE attribute. Remember to enclose
the style properties within quotation marks.
P or
EM) that you are modifying goes first, then an
opening brace, then the style properties, then a closing brace.
Spaces and line breaks are ignored, so feel free to format this in
a visually pleasing way. As always, clear and consistent
indentation is very helpful for any reader of your styles.
Conventionally, the tag is upper case and the style properties are
lower case.
STYLE tag, while external style sheets always omit
the STYLE tag.
STYLE tags. You'll have nothing in angle brackets in
an exteral CSS file.
/* to
begin and */ to end. Thus, for an external style
sheet, you might have:
/* Written by the CS110 Faculty
* Fall 2005
*/
EM {
color: magenta; background: white
}
Using your little.html web page
from earlier exercises, modify the EM tag so that it is
red, the STRONG tag so that it is blue and the
P tag so that it is green. Try all three ways, so make
the P tag green in an external style sheet, the
STRONG tag blue in a document-level style sheet, and the
EM tag red using an inline style. Try nesting them for
fun effects.
CSS has “cascading” in the title because there's a
strict hierarchy that determines which specification applies if the
style of a particular tag is set in more than one place. Suppose you
specified the EM tag as green in an external style sheet,
blue in a document-level style sheet and red in an inline style.
Which wins?
The simplest answer is that the closest specification wins. This makes sense, because you can use the more local specifications to override the more distant, global specifications. So, inline style attributes beat document-level sheets, and document-level sheets beat external style sheets.
One of the most common rules of thumb for creating clear and beautiful documents is consistency. For example, section headers should always be the same size, weight and font. Indeed, this is what a “style sheet” means in the context of document processing. MS Word format files are the same thing.
You can best achieve consistency by specifying the style far enough from the text so that the style is specified in just one place. If you have a single web page that you would like consistent, you should use a document-level style sheet. If you have several web pages (a web site) that you want to be consistent, you should definitely use an external style sheet.
In this course, you and a partner will be creating a web site for a
client. For that web site, you should define an external style sheet.
Then, if you or your client decides that H1 should be
centered, H2 should be bold and blue, H3
should be underlined and non-bold, and EM should be pink,
you can do that in one place. If you later change your mind and
decide that EM should be purple, you make one change in
one file and all of your web site is consistently changed. If you had
used the local style technique, you would have to edit every page of
your site, searching for and modifying each occurrence. This is
tedious and error prone.
Because the CS110 faculty would like our (very large) course web site to be consistent, we will be using a CS110 style sheet throughout. (But we may not have fixed all the web pages yet; we're migrating from a non-CSS web site.)
The idea of CSS illustrates an important concept in the field of
computer science, namely abstraction. Abstraction is the idea of
saying what you want done without saying how to do it. By leaving out the
details about how to do something, you allow for some flexibility, so that
you can revise your method of doing the task without messing up anything
that depends on that task being done. Here, we said EM without saying what
that meant, putting elsewhere the specification of how to emphasize.
So far, we've achieved an effect by modifying one of the existing
tags, mostly EM. Suppose you want to make some text a
different color, but it's not emphasis, so it feels wrong to use
the EM tag. As another example, in the title of the color lecture we used several colors of text
(just to be cute). How did we do this using CSS? The answer is a new
tag called SPAN. By itself, SPAN does
nothing. Its only purpose is to support the STYLE
attribute so that you can modify the appearance of the stuff between
start and end SPAN tags. It's a pretty intuitive name
for the tag. If you view source, on the color lecture, you
can see how we did this.
Suppose that we're using a special word and we want just that special word to be rendered differently. We could say:
<span STYLE="color: teal; background: white">
Supercalifragilisticexpialidocious
</span>
and the result would look like:
Supercalifragilisticexpialidocious
Thus, we don't have to press EM or STRONG
into a job they're not suited for; we can just use SPAN
whenever we want to modify the style of something.
SPAN is intended for stuff within a paragraph, so when
you use SPAN there is no break in the paragraph. If you
want to start a new paragraph, you are probably defining some
big division of your document and you should use the DIV
tag. For example, you might be defining the appearance of the
abstract, the bibliography, a theorem, a glossary entry, an exercise
or something like that. For that purpose, you can use the
DIV tag. Like SPAN, it exists primarily to
carry the STYLE attribute. (There are other uses that
we'll omit.) The difference is that DIV is a
display object, meaning that using it is separated by
vertical space from the preceding and following material, like a
paragraph.
There is an important pitfall in using SPAN and
DIV this way. Suppose we are writing a web page that
involves a number of special terms. Maybe they're trademarked terms,
for example. If we use SPAN each time they occur, we can
control how they look, but if we later change our minds about the
presentation, we'd have to go back and find all occurrences and change
them. This is just the thing that CSS should solve.
We want to set these off somehow, but we don't want to use
EM or STRONG or even DFN (there
is such a tag, used for terms you are defining). We wish there were a
tag called TM, but there isn't. Then, we could modify
the style for TM and use that.
One option is to use style classes. For example, in our external or document-level style sheet we could say:
<STYLE type="text/css">
SPAN.tm
{
color: maroon;
background: white
}
</STYLE>
And within the document, we can say:
Remember that <span class="tm">Coke</span> and <span class="tm">Pepsi</span> are both registered trademarks.
With the result looking as we want:
Remember that Coke and Pepsi are both registered trademarks.
If we later decide that instead of maroon, all the trademarked items should be red or even black (so that they no longer stand out), we can again change this in just one place.
Note that we defined our class by what the item is, not what it looks like. The code of our document is only about the content, not the appearance. Appearance information is only in the style sheets. This separation of content from appearance is an important aspect of the concept of abstraction, which we discussed earlier.
Consider the alternative. Suppose that instead of defining a class
called tm for our trademarked terms, we'd defined a
maroon class and used that. If we later changed our
minds and decided to make them pink, we'd have the bizarre result that
the class named maroon makes things pink. Read more
about good class
names.
We use DIV and SPAN in this way, using
the ex class, to define the format for exercises in these
lecture notes. View the source to see the markup tags.
There are two facets to the syntax for defining and using style classes:
SPAN, DIV, EM,
H1 or whatever. Then a dot (period) and then the
name of the class you are defining. There are no spaces on either
side of the dot. Traditionally, the name of the tag is uppercase
and the name of the class is lowercase, but this is merely
convention. Then define the style as usual within braces.
class, then an equals sign (spaces are
allowed but usually omitted), then the name of the class in quotation
marks.
Among the many kinds of attributes you can specify using CSS, many of the important ones are lengths. You can specify, for example, the width of an element (a paragraph or a document), the length of an indentation, the distance before an element, and so forth. For example, we indented this paragraph by half an inch. It's the only indented paragraph in the site. We did it using the following code:
<p style="text-indent: 0.5in">Among the many kinds of attributes you
You might guess that in means “inches.” To
specify lengths effectively, you need to know the units that
CSS uses. They come in several categories:
in), centimeters
(cm), and millimeters (mm).
px.
pt. A pica is 12 points (“a pica
font”) and is specified by using pc.
em
and ex. The em measure is named for the
width of the letter “M” in the current font, and in CSS is
defined to be equal to the size of the font. The ex
measure is named for the height of the letter “x” in the
current font, and in CSS is usually about half the size of the font.
<body style="width: 50%">
Here is a nice table of these units.
Using your little.html from earlier exercises, modify
the BODY tag so that it is 80 percent of the page, and
modify all the P tags to have a 5em indentation. For
extra credit, make the BODY tag have a left margin of 10 percent,
thereby centering the content on the page.
You should consult this web reference on Centering Things in CSS
This paragraph sets the text-align property to
center so that the lines are centered. How ever, if you
just let the paragraph ramble on, you get ragged edges both left and
right and that doesn't look very good. It's also hard to read. You
can still insert explicit line breaks with the BR tag, so
that if you want to do poetry, you can:
I've never seen a purple cow
I hope I never see one
but I can tell you anyhow
I'd rather see than be one
display
as a block, and sets the margin-left
and margin-right
to auto.
Here are some examples of using CSS to customize a tag.
This paragraph sets the text-align property to
right so that the lines are flush-right.
I've never seen a purple cow
I hope I never see one
but I can tell you anyhow
I'd rather see than be one
<body style="width: 640px; height: 460px"> </body>
This paragraph has
all four margins adjusted:
style="margin-top: 5px; margin-bottom: 10px; margin-left: 20px;
margin-right: 40px; color: black; background: yellow"
list-style-position: inside to make
the bullets be run in to the paragraph, instead of being outside,
which is the default behavior. For more, see the CSS2
reference on lists
display property to inline. I don't
know why the bullets disappeared. Read more about lists in CSS. If
you learn how to have horizontal bullet lists, let us know!
By adding a style="float: left" to that IMG
tag, we can allow text to flow around the image. I also added
margin-right: 2em so that there's a bit of space between
the image and the text. Does that image seem vaguely malevolent?
Halfway to the deadly snake-eyes in craps? Or am I just rambling on
in order to have some text to flow around the image?
A:link
{
color: lime;
background: white
}
The :link part is called a
pseudo-class.
:visited
pseudo-class for the A tag:
A:visited
{
color: olive;
background: white
}
SPAN with margins: this text has 50px left margin and 40 px top
margin and now the span is done. Notice that the left margin
does adjust, but the top margin doesn't. SPAN just
doesn't seem to create the same kind of box that DIV does.
We have only scratched the surface of CSS. There is much more to learn. You can learn more from