CS 4, Summer 2006: Lecture 4: HTML and Styles

Overview

Styles

There are many properties of physical appearance we might want to control, once we have the structure of the document right.

Which color

How do we tell a computer what color we mean?

What to color

Which font

Now, how about how the text itself appears? You're probably already familiar with the notion of font, which is just a collection of letters, digits, and special characters. We can specify within a style a font-family property value, e.g.,

Here are some examples. Specifying a font is kind of a "suggestion", which the browser will try to follow depending on what's installed on the system.

<span style="font-family:Arial"> Lorem ipsum dolar ebit rabis sabif fa odoloar
<span style="font-family:Impact"> Lorem ipsum dolar ebit rabis sabif fa odoloar
<span style="font-family:Times"> Lorem ipsum dolar ebit rabis sabif fa odoloar
<span style="font-family:Verdana"> Lorem ipsum dolar ebit rabis sabif fa odoloar

So, styles let you set fonts, and as mentioned above for color, sheets (below) provide the most power and flexibility in specifying them. While its use is now discouraged, the <font> tag historically did that, on an individual basis. Since you might see this tag, here's the basic idea.

Style sheets

Some HTML tags specify physical properties of the text they contain (e.g., <b>, <i>). Some HTML tags have attributes (e.g., bgcolor) that let you specify display properties. We can specify style property:value pairs in many ways. But setting all of these values for every tag quickly gets tedious, and making changes to even a modest-sized website is a lot of work if you do it this way.

Thus the clever folks at the W3C borrowed the old typographer's notion of a stylesheet. A stylesheet is basically a list of rules of the form: "Whenever you see tag X, use this style information."

The exact same text can look very different, depending on the stylesheet. See the CSS zen garden, for example. It follows the reasoning we discussed in logical vs. physical tags -- specify physical appearance separately from structure.

Some simpler examples than the Zen Garden:

This is really only scratching the surface. Stylesheets give you a lot of control over the appearance of your document, without forcing you to destroy the spirit of HTML as a markup language. Of course, the degree to which CSS is supported varies from browser to browser, but the basic components should work for virtually all reasonbly recent browsers.