Unit 4: GoLive, (X)HTML, and CSS
| ! | Lesson Goal: Learn why cascading style sheets are the way to go and how to use GoLive's CSS Editor. |
Cascading style sheets (also called CSS) contain "styles," instructions that tell the browser how to display the elements on the associated Web page. The W3C recommends the use of styles to replace deprecated tags and to handle presentation issues (as opposed to structural issues, best handled within the HTML code itself). In this lesson, you'll learn the basics of style sheets, including how to use GoLive's built-in CSS Editor and Style Inspector.
If you look at the overview of the Web Tech Curriculum, you'll see that there are numerous lessons on cascading style sheets. As a result, you may be asking yourself, "What's the big deal about style sheets? Why are they so important?"
Well, the answer comes from the evolution of HTML recommendations. When the W3C releases a new version of HTML recommendations, certain elements are deprecated and/or obsoleted. Deprecated elements still work in most browsers (but may be on their way out); obsoleted elements may not work in browsers at all. In the W3C's own words:
A deprecated element or attribute is one that has been outdated by newer constructs... Deprecated elements may become obsolete in future versions of HTML...
An obsolete element or attribute is one for which there is no guarantee of support by a user agent.
~ W3C HTML 4.01 Specification (http://www.w3.org/TR/html401/conform.html#deprecated)
You've been learning about the current W3C standard, XHTML. Many elements from previous HTML versions (HTML 4.01 and earlier) may be considered deprecated or even obsolete by the W3C. According to the W3C, style sheets are the new big thing and are quickly becoming the de facto standard.
Your first real exposure to deprecated elements in this curriculum came in the previous lesson Entering and Formatting Text in GoLive. For example, the font element, used for some of the basic GoLive text formatting options, was deprecated in the HTML 4.0 specs in favor of styles. If you recall, the font element is used by GoLive when applying size and color to text. This might bring another question to mind: "Why does GoLive use deprecated elements?" We think at least part of the answer coincides with the reasons that it is important to know deprecated elements:
But, don't worry Adobe wouldn't leave you stranded using deprecated elements. GoLive provides an extensive cascading style sheet editor. The various cascading style sheet lessons in this curriculum will bring you up to speed on the various types of style sheets, how and when they should be used, and how to create and apply styles in GoLive. Once you start working with styles, you will forget all about using deprecated formatting options and use styles within GoLive. Styles will become your de facto standard for handling Web page presentation issues, not just because they are the W3C standard, but because you will be amazed at their ease of use and flexibility.
CSS vs. HTML Alert
Be aware that style or CSS "language" is not the same as HTML markup language, so the code you are about to view looks quite different!
Cascading styles can be used in various ways:
Inline, also called styles applied locally
Example opening tag using style attribute:
<h1 style="font-family:sans-serif; color:#996633">
Internal style sheets (also called document-level style sheets), which reside in the head section of an HTML document
Example style element coding:
<style>
<!
body {font-family:Arial, sans-serif; color:green}
>
</style>
By the way, the term "cascading" refers to the cascading order of precedence for these various levels of formatting changes. In general, the formatting changes closest to the affected element have precedence. Let's use the h1 element as an example.
h1 headings display.h1 headings should display, the CSS file takes precedence over the browser default.h1 headings should display, that document-level style sheet takes precedence over both external CSS files and browser defaults.h1 heading, they take precedence over all other formatting specifications.Using external style sheets is an excellent way to maintain consistency among the pages of your Web site. Here are some of the advantages:
h1 headings one time, in one place.So what's the down side? There is one major one:
This means you need to stay aware of which browsers support which features and design accordingly. For example, you may wish to include only those features which are supported by the last two or three versions of both of the two major browsers. The good news is that, generally speaking, each new version of the major browsers includes more CSS support. As users update their browsers, this problem will decrease substantially.
As discussed previously, inline styles are also sometimes called styles applied locally. Because they are formatting changes applied closest to the affected element, they have precedence over the other style types. You've already seen how the deprecated font element can be used to format inline text. The newer, preferred way is to use the style attribute inside the opening tag of the element you wish to format. For example, you might include the style attribute inside an opening <p> or <h1> tag, in order to specify desired changes to the font color, size, etc.
The basic syntax for the style attribute is:
<elementname style="property:value; property:value;">
Note the following:
property:value pairs are specified, there's only one set of quotes around the whole list (not around each pair individually).property:value pair is optional, it is good practice to include it.In the following example, the opening <h1> tag includes the style attribute, which is used to set the heading's font properties:
<h1 style="font-family:Arial,Helvetica,sans-serif; color:#000080">Heading One</h1>
which renders as:
The properties used by the style attribute that affect text's typeface, size, and color are font-family, font-size, and color. Again, notice the difference in the syntax and terminology of styles compared to basic HTML.
Let's compare the HTML code using inline styles versus the deprecated font element to control the size of paragraph text:
style attribute |
font element |
|
|---|---|---|
| HTML Code |
<p style="">Relative Font Size 175%</p> |
<p><font size="+2">Relative Font Size +2</font></p> |
| Browser Rendering |
Relative Font Size 175% |
Relative Font Size +2 |
As you can see, the style attribute used by inline styles works just like any other attribute previously discussed. The difference is in the terminology (e.g., font-size) and the syntax of the information within the quotes (e.g., "property:value;").
What if you don't want to format a whole block (e.g., a whole paragraph, a whole heading, etc.), but only a few words within a block? The span element, along with the style attribute, affords the opportunity to affect a particular selection of text. By itself, the span element doesn't really do anything to the text contained by it; it gains its "power" from the attributes it utilizes, e.g.:
<p>I want these <span style=" color:purple;">large, purple words</span> to stand out.</p>
which renders as:
I want these large, purple words to stand out.
IMPORTANT
Use inline styles sparingly; most of your styles should be contained within an external style sheet, in order to achieve clean code and maximum efficiency.
Internal style sheets (also called document-level style sheets), reside in the head section of an HTML document. Unlike inline styles, internal style sheets use the style element, rather than the style attribute.
The basic syntax for the style element is:
<style>
<!
elementname {
property:value;
property:value;
property:value;
}
>
</style>
Note the following:
<style>...</style> container contains all of the formatting instructions.style tags in order to hide styles from those browsers that don't support them. Otherwise, the formatting instructions will show up on the Web page as though they were part of its intended-to-be-viewed content.property:value pairs are enclosed within one set of curly braces { }.property:value pair on its own line, but it does help increase readability.In the example below, the style element (set inside a document's head section, where it belongs) sets the body text's default typeface, size, and color:
<head>
<title>title text</title>
<style>
<!
body {
font-family:Arial, sans-serif;
color:green;
}
>
</style>
...
</head>
An external style sheet is simply a stand-alone text (ASCII) file with a .css extension. For example, the CSS file used on this course Web site is named "adobe_style.css."
Since CSS files are not HTML files, their contents do not need to be enclosed in html tags or have head and body sections. They simply start with the first element for which you wish to set a style.
CSS files use a syntax similar to that of the style element discussed previously, e.g.:
h1 {property1: value1;
property2: value1,value2;
property3: value1;
}
Note the following:
h1 element).property:value pairs; each property is separated from its value by a colon (:), while the pairs are separated from one another by semicolons (;).property3's value1 in the example above) is not required, though it is recommended (in case you want to add another property later).Comments may be entered using the following syntax:
/* comment goes here */
So, for example, if I want all my h2 headings to be large, blue Arial (or other sans serif font), the h2 element in my style sheet will look something like this:
h2 {font-family: Arial, sans-serif;
color: #083194; /* dark blue */
}
You can even include more than one tag name in a specification if it's more efficient to do so. For example, if I want all my headings to be dark blue and Arial, I could lump them all together to specify those properties, then create individual specs for the properties that differ among the headings (in this case, the font sizes):
h1, h2, h3, h4
{font-family: Arial, sans-serif;
color: #083194; /* dark blue */
}
h1 {font-size: x-large;}
h2 {}
h3 {}
h4 {}
Once you've created an external style sheet (a .css file), you can link to it from as many different HTML documents as you wish. Let's say you've named your CSS file "style1.css" and you want every page on your site to use that style sheet. In each HTML document's head section, usually after the title line, insert the following line:
<link rel="stylesheet" href="style1.css" type="text/css">
Note the following:
rel="stylesheet" means the referenced CSS file is related to the HTML document as a style sheet.href="style1.css" gives the path and name of the CSS file.type="text/css" tells the browser that the referenced file is a text file which follows CSS conventions.A very powerful feature of CSS is the ability to use a class name to represent or assign a specific set of style changes. The class you create can be a generic class (applicable to multiple elements) or an element-level class (applicable to a specific element).
Let's say I teach an online course. In my course Web site's CSS file, I have my h1 and h2 headings set as follows:
h1, h2
{font-family: "Times New Roman", serif; color: magenta;}
h1 {font-size: x-large;}
h2 {}
I want all my h1 and h2 headings to display in left-aligned, magenta Times New Roman except when I use h1 for my course number (EDUC 110) and h2 for my course name (Learning Theory & Instructional Design); in that particular case, I want them to display in centered, blue, italic Arial. Therefore, my CSS file also contains the following lines, which create a generic class named "courseid." Note that generic class names must be preceded by a period:
.courseid
{color: blue;
font-style: italic;
font-family: Arial, sans-serif;
text-align: center;
}
Now that I have the class in my CSS file, how do I tell the HTML pages when to use it? Generic classes are utilized via the class attribute of an element's opening tag. In the body of my course Web site pages, I make use of that "courseid" generic class whenever I start the page with my course number and name:
<h1 class="courseid">Education 110</h1>
<h2 class="courseid">Learning Theory & Instructional Design</h2>
Since my "courseid" class doesn't specify a font size, these h1 and h2 headings inherit their font sizes from the h1 and h2 specs elsewhere in the CSS file.
If you want a whole section of a Web page (e.g., several paragraphs or a whole table) to be affected by the same generic class, you can enclose that section within <div> tags. The div element is a block-level element. It helps to add structure to documents; however, the content contained between the div tags does not appear visually different in a browser when used by itself. (FYI, this element can also be used with the id or style attributes.)
The following example would render both the paragraph and the table between the div tags in centered, italic, blue Arial (because that's how I defined the "courseid" class in my CSS):
<div class="courseid">
<p>...paragraph text here...</p>
<table>
...table tags here...
</table>
</div>
The span element, typically used with inline text, can be used in conjunction with a CSS class. It identifies an individual section of text (e.g., a word or two) for special formatting. For example, if I occasionally want to call attention to certain words by making them bold and green, I could create the following class within my CSS file:
.boldgreen
{font-weight: bold; color: green;}
Then, within my HTML document, I utilize the opening <span> tag's class attribute to format the particular words I want to emphasize, e.g.:
<p>Pay particular attention to the <span class="boldgreen">span</span> and <span class="boldgreen">div</span> tags!</p>
which would render as:
Pay particular attention to the span and div tags!
In some cases, you may wish to create classes for two or more different formats for the same tag. In this case, the name of the element is followed by a period, which is followed by the name of the class. For example, if you need two different p formats in addition to the default, one bold and centered and the other left-justified and italicized, here's what would go in your CSS file:
p.centered {font-weight: bold; text-align: center;}
p.left {font-style: italic; text-align: left;}
Here's how you would use these element-level classes within your HTML document:
<p class="left">This paragraph is left-aligned and italicized.</p>
<p class="centered">This paragraph is centered and bold.</p>
which would render as:
This paragraph is left-aligned and italicized.
This paragraph is centered and bold.
*** Begin section copyright © 2002-2003 Adobe Systems Incorporated ***
Now that you have a general understanding of the various style types (i.e., inline, internal, and external) and style classes (i.e., generic and element-level), it's time to learn how to create internal styles that can be applied to text in GoLive. GoLive uses the same interface to create internal and external styles, so once you learn to create internal styles, creating external styles will be a snap!
IMPORTANT
GoLive has what it calls HTML Styles. HTML styles are not related to cascading style sheets. They are typically used to apply more than one attribute to a text element. However, if the style is updated, the new stylistic changes will not be automatically applied to the text upon which the style was previously applied. In addition, many of the pre-defined HTML styles, as well as styles created by the user, tend to utilize deprecated HTML elements and, therefore, are not a preferable "style" option.
The first step in creating an internal style sheet in GoLive is to open the CSS Editor. The CSS Editor button
is located in the upper right corner of the Document Window, as well as in the Document Window Toolbar. As shown in Figure 4.3-1, the CSS Editor stores both internal (e.g., h1) and external (e.g., basic.css) styles. All new sites created in GoLive automatically include a default external style sheet called basic.css. You will learn to edit the exteral css file in a future lesson. Note the two small icons
below the CSS name information. The icons, Show/Hide Split Source and Show/Hide Style Preview, respectively allow for a split screen view of either the CSS source code or a preview of the applied styles. The buttons in the CSS Editor include the following:

Figure 4.3-1: CSS Editor
The buttons in the CSS Editor include the following:
h1) before formatting options are selected.The CSS Style Inspector is used to define a new style, i.e., to specify the style's property:value pairs. It appears when a new style is created in the CSS Editor. Since we are focusing on type-related styles at this time, let's examine the CSS Style Inspector's Font Properties and Text Properties buttons.
The CSS Style Inspector Font Properties Tab (see Figure 4.3-2) appears when you click the Font Properties button. It provides various font-related attributes that can be assigned to the new style, including color, back color, size, style, line height, decoration, and font family.
Remember, not all computers have the same fonts installed; choosing the perfect decorative font might backfire if the user doesn't have that font on his or her computer.

Figure 4.3-2: CSS Style Inspector Font Tab with Style Preview Displayed
The Text Properties button of the CSS Style Inspector (see Figure 4.3-3) is used to define such things as text indentation, word spacing, letter spacing, alignment, etc.

Figure 4.3-3: CSS Style Inspector Text Tab
Before we go any further, let's answer one of those questions that has to be in the back of your mind, "What are the various measurement options all about?" You've seen the same measurement options from field to field in the CSS Style Inspector. Here's a brief summary:
IMPORTANT
Relative measures are almost always preferred over absolute measures for scalability and accessibility reasons; e.g., if you set a font size to be only 12 pixels tall, a user with low vision may not be able to use the browser to enlarge the text.
Once created, element styles are immediately updated in the Layout Editor. This makes sense if you remember that this type of style reformats the attributes of existing elements. For instance, if a new h1 style is created to reformat the default h1 attributes, any text assigned to the h1 element will automatically take on the new formatting.
To create element styles, follow these steps:
Class styles are a bit different; they don't get applied until you specify where the class is to be used within the HTML document (explained below).
To create class styles, follow these steps:
Applying classes to text requires the use of the Apply CSS Style button in the toolbar or the CSS Palette. Using either the Apply CSS Style button or the CSS Palette, the style names appear on the left with four labeled columns to their right: Inline Style, Block Style, <p>, and <body>. A fifth option (<span>) may appear when the Inline Style checkbox is selected. You should recall from previous discussions that span is used specifically for inline styles (singling out a range of text) and should not be used to effect entire blocks of text. Finally, the Block style column is used when the style is to be applied to multiple blocks of text. The <p> column relates to paragraphs and will therefore affect all text within a designated paragraph element.
To apply class styles, follow these steps (these steps assume that the class has already been created):
Demonstration or Practice Activity
For this activity, you will create and apply three element styles and one class style.
*** End section copyright © 2002-2003 Adobe Systems Incorporated ***
Copyright © 2002-2005 Adobe Systems Incorporated, except those portions marked copyright © 2000-2005 ID 4 the Web. All rights reserved.