(Last Updated 11/27/98)

(Cascading Style Sheets Part 1)
Introduction

A Little Class - A Bit of Elan - A Touch of

STYLE



This is a short introduction to adding Style to your Web pages. It is intended to provide a taste of programming in the CSS language and how to integrate it with standard HTML. The good news is, with CSS many new and dynamic effects are possible in web documents. The bad news is CSS is essentially an entire new markup language in and of itself.

STYLE is the term used for a group of properties that define how an HTML element or document will appear when viewed in a browser. A Style Sheet (not necessarily a contiguous page or sheet) describes the element's default characteristic set of styles. Style Sheets are normally derived from multiple sources and have a specific order of precedence when the definitions of a style option conflict. Basically, style sheets append style or layout rules to HTML tags by:


  1. attaching directly to the tag so each time the tag is used, the style rules are applied;
    ex.   The style declaration   H5 {text-align: center},
    applied as   <H5>Title 1</H5>,
    is identical to the HTML statement   <H5><CENTER>Title 1</CENTER></H5>.

  2. defining a named class of instances when the style is to apply and attaching the class name to any tag that it applies to;
    ex.   The style declaration   .a1 {text-align: center},
    applied as   <H5 Class="a1">Title 1</H5>,
    is identical to the HTML statement   <H5><CENTER>Title 1</CENTER></H5>.

  3. defining a selector ID that is applied to relevant tags.
    ex.   The style declaration   #b1 {text-align: center},
    applied as   <H5 ID="b1">Title 1</H5>,
    is identical to the HTML statement   <H5><CENTER>Title 1</CENTER></H5>.

Pseudo classes (defined below) may also be used in specific cases.

Basic Concepts

Grouping
Grouping provides for the assignment of a single style declaration to multiple elements.
Example: H3,H4,H5,H6 { margin-left: 2% }
This provides a left margin of 2% of the window width to apply to the use of elements H3, H4, H5 and H6.
Inheritance
Inherited values are values passed from a parent element to a child element.
Child elements are elements enclosed by the start and end tags of the parent.
class
Class are used to select data items (objects). A Class is defined by a name preceded by a dot. It is called using the "STYLE=" attribute. The dot does not appear in the value of STYLE.
Example - style defined: .colorRed { color: red }
Example - style used:     <P class="colorRed">This text is red.</P>
Pseudo
Class
Pseudo-classes are fictional element types. In CSS1 only the anchor element (A) can act as a pseudo-class. Normally 3 types of fictional A elements are created, the: Unvisited Link, Visited Link, and Active Link. Pseudo links can be combined with normal classes to choose different styles for different link sets.
Example - style defined: A:link { background: transparent; color: blue }
Example - style defined: A:visited, A:active { background: transparent; color: red }
Example - style used:     <A HREF="...">title</A>
Example - style defined: A.foot:link { background: transparent; color: blue }
Example - style used:     <A Class=foot HREF="...">title</A>
ID
IDs are used to select data items. An ID is a string preceded by a "#" symbol. It is called using the "ID=" attribute. The "#" does not appear in the value of ID.
Example - style defined: #Item2 { color: red }
Example - style used:     <P ID="Item2">This text is red.</P>
Select by
Context
Used when styles are to be applied to an element when it is used in a specific context. They are created and used in a parent-child context. The style element defined is applied to the child when it appears with its parent.
Example - style defined: H5 EM {color: red }
Example - style used:     <H5>This </EM>color</EM> is RED </H5>
                                                but This </ EM > color </EM> is NOT.
first-line
Pseudo
Element

Applies only to
block level
elements
First-line pseudo-elements allow sub-parting the element's first line and attaching a specific style exclusively to it. It may be combined with normal classes.
Example - style defined: P:first-line {color: red }
Example - style used:     <P>This line is red. The rest aren't. </P>
Example - style defined: P.initial:first-line {color: red } {Ex. with class="initial"}
first-letter
Pseudo
Element

Applies only to
block level
elements
First-letter pseudo-elements allow sub-parting the element's first letter and attaching a specific style exclusively to it. It may be combined with normal classes.
Example - style defined: P:first-letter { font-size: 200%; color: red }
Example - style used:     <P>This letter is red and twice as large.</P>
Example - style defined: P.initial:first-letter {color: red } {Ex. with class="initial"}




Linking CSS Commands to HTML

Style definitions may be incorporated in HTML in four ways. These are:

These documents will discuss and provide examples for each of these methods. However, the documents themselves are based upon the first two methods.

As with all languages, CSS has its own "style" or notation. Aside from Class and ID declarations, each style declaration starts with a tag name followed by a list of style properties enclosed in braces. The properties are listed within the braces followed by a colon and the value for the property. Each "property/value" pair is separated by a semi-colon. As an example:

<STYLE type="text/css">
    ...
    BODY {
        color: black;     background: aqua;
        margin-left: 5%;     margin-right: 5%;
        font-family: Helvetica, "Times New Roman", sans-serif;
    }
    ...
</STYLE>

In this snippet, the STYLE element is used to set (some of) the properties in the documents BODY statement. These are: set the text color to black and the background color to aqua; place 5% (of the window width) margins on either side of all standard text; and use the Helvetica font. It additionally says that if the Helvetica font isn't available, use Times New Roman, or as a last resort if it isn't available, Sans-Serif. This was copied from this document's style sheet. Note that for any items defined in both the Style Sheet and in the document <BODY ...> statement, the Style Sheet values take precedence.

It is seen that names were used for the colors. This is only my habit. Numeric values such as "rbg(255,0,0)" or Hex notation "#FF0000" may also be used. In these notations the numbers refer to the amount of red, green, and blue tint in the color. In decimal notation the number range from 0 - 255, and in Hex from 00 to FF with the latter (255 or FF) being maximum for that color. A more complete discourse on color may be found at Web_Tint.html (when I write it).

A look at the page source also reveals another interesting item in this example. The Font size, style, and color change was defined in the Style Sheet in element PRE. This was done because it is anticipated that many such examples will be used in the document.
i.e. The actual coding uses the tag notation:

<PRE> example code </PRE>
Rather than:

<FONT SIZE=-1 COLOR="black"><B> example code </B></FONT>

A lot easier. It is also a good idea to stop using the FONT tag in your HTML pages as it is now considered a deprecated (old - no longer supported) tag.

Three "gotchas" were found in writing this page:

  1. To use the old HTML cloaking of the style sheets in the header (to hide them from old browsers) it was necessary to put in an open / close brace set at the end of the cloaking line or the first style element would be ignored. The is probably a result of the change in comment identifiers (to C/C++ style).
  2. If the PRE element doesn't start a new paragraph (be preceded by <P> or </P> or <BR><BR> it inherits the font size from the previous paragraph (it doesn't change it. It does however change the style and color attributes however). Certainly curious why the double BR would work.
  3. Finally, when using the predefined styles, there were instances where carriage returns in the text were no longer ignored in the presentation. This seemed to be more prevalent in using the Style Attribute. Curious'er and curious'er.



External style sheet

If it is likely that the same styles will be used in several documents an external style sheet should be considered. It can be attached to a document either by Linking a full sheet or by importing additional styles to compliment the ones defined in an internal style sheet.

To use the Link Element, the Style Sheet must be a separate document. Linking is accomplished by including a line of the form:

<LINK   REL="stylesheet" HREF="externalStyle.css" TYPE="text/css">

in the header section of the document. The "REL" attribute must be set to "stylesheet" for the sheet to load correctly.

When a document needs "additional style", beyond what is in the document's style sheet, the @import declaration is used. It must be the first declaration in document's Style Sheet. Local declarations will take precedent. It is legal to use multiple @import declarations. In that situation each subsequent declaration over-rides the preceding one when conflicts arise.

<style type="text/css">
    @import "anotherStyle1.css";
    @import "anotherStyle2.css";
    H1 { margin-left: -5%;}
    ...
</style>

finis


Other CSS & CSS Related pages:



Top of Page


Contact Carl Paulson with questions,
or Visit my pages.


Other Programming & Scripts Pages

| Main Page | Java | C++(not yet active) | HTML |
| Java Script | mIRC / LittleStar | IRCLE | Shadowbot | Apple |