Cascading Style Sheets (CSS)

By Prof. Marc Abrams, Computer Science Department, Virginia Tech


Content:

  • References
  • Introduction/history
  • Simple CSS
  • Examples of CSS
  • Browser Settings Related to Style Sheets
  • Complex CSS
  • Exercises for you to try!

  • 1. References:

  • [BO] David D.Busch & J. W. Olsen, Cascading Style Sheets - Complete, McGraw Hill, 1998.
  • [W3C] WWW Consortium, Web Style Sheets.
  • [Activity] WWW Consortium, W3C activity: Style Sheets.
  • [Spec1] Håkon Wium Lie, Bert Bos, Cascading Style Sheets, level 1, W3C Recommendation 17 Dec 1996, REC-CSS1-961217, rev. 11 Jan 1999.
  • [Spec1] Cascading Style Sheets, level 2, W3C Recommendation 12 May 1998, REC-CSS1-980512

  • 2. Introduction and History

    What is Web Page "Style"?

    When you view a Web page, the page has certain visual properties or presentation formats: colors, fonts, background colors and textures, shape of bullets in lists (e.g., circle or square), and so on. These properties are known as style. In fact, all of the properties needed to lay out the visual appearance of a Web page comprise the style properties. These include properties like the distance between lines of text on the screen, the size of margins, and the amount of white space around page elements like images.

    Prior to HTML version 4, style information was "hard-wired" into an HTML document, for example using the <font> tag or attributes like bgcolor in the <body> tag. This is undesirable if the same HTML document is to be displayed on computer monitors of different sizes and resolutions, or on mobile devices such as PDAs and handheld computers. To make a document portable to allow different presentations on different devices, one would like to associate multiple styles with a single HTML document. Therefore the style information should separated from the rest of the HTML document, and this is what a style sheet does.

    A final motivation for style sheets is accessibility of Web pages for people with disabilities. For example, a blind individual could use a Web page if a style sheet were created that described auditory properties, rather than visual properties. For example, a bullet in a list could be presented as a tone in a Web browser that speaks the content of a Web page, rather than a black circle in a traditional Web browser.


    Brief History of HTML and Style Sheets


    SGML

    Prior to the development of HTML, there was an ealier markup language in use: the Standard Generalize Markup Language, or SGML. SGML is very powerful, but the language is complex and tools for SGML were not widely available. Therefore few people used SGML.

    HTML

    With the proposal of the Web in 1989 at CERN, a simplified version of SGML was proposed for representing Web pages: the hypertext markup language (HTML). HTML in its early versions (1 and 2) was a simple language that contained a small number of tags.

    Today, HTML is viewed as a special case of an SGML document. This is because HTML is a Document Type Declaration (DTD) of SGML.  That's why some Web page authoring tools, such as Netscape Composer, will insert a line near the top of an HTML document similar to the following:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">


    The Problem with HTML 3.2

    The sudden popularity of the Web in the mid 1990's created a crisis for HTML. On the one hand, the original goal for the Web at CERN was to have documents that could be displayed on any type of computer (e.g., UNIX, Macintosh, DOS and later Windows). Therefore HTML 1.0 and 2.0 described content, and not presentation format. On the other hand, a number of Web page designers creating Web sites wanted control over the exact appearance of a page in a user's Web browser. That control was important to achieve high-quality graphic designson the Web similar to what on sees in printed publications. These designers needed to specify what we know today as style.

    To woo Web page designers to their products, Netscape and Microsoft addeda short-term solution to HTML. They added non-standard tags and attributes specifying formatting information to HTML, violating the rule of content and not format. An example is the <font> tag:

    <FONT=+2>  </FONT>

    The problem with tags like <font> is that they are interleaved throughout an HTML document, aggrevating the problem of creating different presentations for different output devices or for accessibility.

    The group responsible for CSS stated the need as follows:

    "On the Web, content providers do not have the control they have in print media over color, text indentation, positioning, and other aspects of style. A style sheet language offers a powerful and manageable way for authors, designers and typographers to create the rich visual effects." [Activity]

    CSS1

    In Spring 1997, the World Wide Web Consortium (W3C) formally proposed a long-term solution: Cascading Style Sheets. A draft specification for CSS version 1 (known as CSS1) was introduced.  CSS1 is used in Netscape's Communicator version 4 and later, Internet Explorer 4 and later, and SGI's browser.
  • Format info appears in either a separate document, or a separate area of a .html file, called a style sheet.
  • A style sheet lists, for many tags (e.g., <P>), how the tag should be rendered.  For example:
  • Render <H1> as red, bold Helvetica 48 pt.
    Render <H1> as a deep man's voice.
  • Tags may be subclassed, so that each subclass specifies a different presentation format (e.g., Regular H1 headings in black versus really important H1 headings in a red font).

  • CSS Timeline


    Why Cascading Style Sheets (CSS)?

    Cascading refers to a hierarchy of style sheets: From CSS1 spec:  "the author's style sheets override the reader's style sheet which override the user agent's default values. An imported style sheet has the same origin as the style sheet from which it is imported."

    Also, you can use the !important annotation (discussed below) to let style on the reader's style sheet override the author's style sheet.  So you could avoid small fonts, render pages as audible, etc.


    3. Simple CSS

    Style Sheet Examples


    Basic Concepts

    Here is a simple style sheet. It specifies that any use of <H1> in the document will generate blue type.

    H1 { color: blue }


    General Syntax

    The general format of a line of style is the following:

    selector declaration


    Grouping

    There are some short-hand notations in CSS. First of all, the selector can list multiple tags:

                  H1, H2, H3 { font-family: helvetica }

    Second, the declaration can contain multiple style properties and values:

                  H1 {
                    font-weight: bold;
                    font-size: 12pt;
                    line-height: 14pt;
                    font-family: helvetica;
                    font-variant: normal;
                    font-style: normal;
                  }

    There are also some special-purpose syntax conventions, such as the use of the traditional typographer's notation for font height and line height:

                  H1 { font: bold 12pt/14pt helvetica }

    In this case H1 headings will use a 12 point font inside a line height of 14 points, so that there are two points of white space (known as "leading") between the lines.


    Class as Selector

    Earlier we mentioned that tags can be subclassed. The example given was the desire to make some H1 headings black and some red. In this case, we want to make two sub-classes of the H1 tag. To do this, HTML 4 allows tags inside <body> to have a new attribute called CLASS. When you author an HTML document, you can define classes of a certain tag (such as H1) by giving names of your own choice to the CLASS attribute.

    In the example below, a class of H1 tags is defined and named urgent. The style sheet uses the selector H1.urgent to specify style for only those H1 tags which have attribute CLASS=urgent. The result of the document below is a red heading that says "I am in red because I am urgent".

                <HTML>
                 <HEAD>
                  <TITLE>Title</TITLE>
                  <STYLE TYPE="text/css">
                    H1.urgent { color: #FF0000 }
                  </STYLE>
                 </HEAD>
                 <BODY>
                  <H1 CLASS=urgent>I am in red because I am urgent</H1>
                  ...
                 </BODY>
                </HTML>

     


    Linking an HTML Document to a Style Sheet

    There are four different ways that a style sheet can be added to an HTML document. In two of the ways, the style sheet is in an external file. (The external file must only contain lines of the form "selector declaration." The external file must not contain any HTML tags, such as <style>.) In the other two, the style sheet is internal to the HTML file.

    The HTML document below illustrates all four methods:

                <HTML>
                  <HEAD>
                    <TITLE>title</TITLE>
                    <LINK REL=STYLESHEET TYPE="text/css"
                      HREF="http://style.com/cool" TITLE="Cool">
                    <STYLE TYPE="text/css">
               @import url(http://style.com/basic);
                      H1 { color: blue }
                    </STYLE>
                  </HEAD>
                  <BODY>
                    <H1>Headline is blue</H1>
                    <P STYLE="color: green">While the paragraph is green.
                  </BODY>
                </HTML>

     

                    <LINK REL=STYLESHEET TYPE="text/css"
                      HREF="http://style.com/cool" TITLE="Cool">                   @import url(http://style.com/basic);               <BODY>
                    <H1>Headline is blue</H1>
                    <P STYLE="color: green">While the paragraph is green.
                  </BODY>
                </HTML>


    4. Examples of CSS

    A Very Simple Example

    Here is a very simple example of an HTML file with an internal style sheet.

    <html>
    <style type="text/css">
      <!--
      BODY {color: red}
      -->
    </style>
    <head><title>Test</title></head>
    <body>
    <h1>Heading</h1>
    <p>This is some text.
    </body>
    </html>
    Click here to see the result!

    Sample style sheet for HTML 2.0  in CSS

             BODY {
               margin: 1em;
                    font-family: serif;
                    line-height: 1.1;
                    background: white;
                    color: black;
                  }

                  H1, H2, H3, H4, H5, H6, P, UL, OL, DIR, MENU, DIV,
                  DT, DD, ADDRESS, BLOCKQUOTE, PRE, BR, HR { display: block }

                  B, STRONG, I, EM, CITE, VAR, TT, CODE, KBD, SAMP,
                  IMG, SPAN { display: inline }

                  LI { display: list-item }

                  H1, H2, H3, H4 { margin-top: 1em; margin-bottom: 1em }
                  H5, H6 { margin-top: 1em }
                  H1 { text-align: center }
                  H1, H2, H4, H6 { font-weight: bold }
                  H3, H5 { font-style: italic }

                  H1 { font-size: xx-large }
     

    Example CSS Stylesheet from Word 2000

    Microsoft Word in Office 2000 can generate HTML documents that use a style sheet. The HTML generated is fairly complex, and this example shows that style sheets can be complex. In the example below, we see a variety of style properties set for H1 headings, such as the use of 16 point Arial font, and properties for anchors in Web pages that are visited -- they are in purple when displayed.



    5. Browser Settings Related to Style Sheets

    Next we will discuss the features that exist in Web browsers to use style sheets.

    In IE5 or Netscape4.x, there are dialog boxes to specify your preferences for colors and fonts.  These in essence form the "user agent style sheet".

    Internet Explorer 5

  • Browser user can
  • To do this, select Tools->Internet Options to expose this dialog box:

    Then click on the "Accessibility" button above to expose the dialog box below:


     

    Netscape 4.6

    You can elect to enable or disable use of author style sheets.

    Choose Edit->Preferences to display the box below, and click on "Advanced":

    Click on "Enable style sheets" to change the setting.

    Hint:  Sometimes you visit a Web site and nothing appears in your browser window.  Try disabling style sheets and reloading the page; this sometimes displays the page (perhaps when the style sheet contained an error).


    6. Complex CSS

    What makes CSS complex is the interference of two mechanisms:

    Inheritance

    HTML document can be viewed as a tree.

    Children of a node inherit properties of their parent -- unless the style sheet or cascading says otherwise.
     

    Example

    If you view the following example in a Web browser by clicking here, all the text will be non-italics.


    Now suppose we add a <style> section as follows:

    If you view the above in a Web browser by clicking here, all the text is italics!
    Note:  This is true if you use Internet Explorer 5.0.  If you use Netscape 4.7, you do not see italics for the heading.  Perhaps Netscape incorrectly implements style, or perhaps there is some option in Netscape that is overriding the style.  No matter what the reason, it shows how hard it is to get consistent-looking documents across browsers!


    Why?  Well, first of all the tree structure of the above html document is this:

    The inheritance mechanism says that H1 and P must have font-style:italic because they inherit from the BODY tag.

    Now suppose you modify the example as follows:

    To see the result, click here.  The sentence "This is some text" is no longer italicized.  That means that explicitly setting an attribute overrides inheritance.
     

    How Inheritance and Cascading Interact

    This is where things get complex!

    What if there is a conflict, and author's style says 18 point, and reader's is 10 point?

    There are several things that complicate this:

    7. Exercises for you to try

    1. Modify this html document to add a style sheet using the <style> tag.  The style sheet should make all headings appear in a red serif font.
    2. Repeat (1), but also make all lines of text double spaced!  (I personally find this a handy trick if I want to mark up with pencil and paper a draft of a document I'm writing as an html document.)  If you get stuck, look at this example.
    3. Modify this document to use your favorite of the W3C Core Styles.  (Note:  These styles look great with Netscape's browser, but some items are mis-formatted by IE5 [namely rules].)
    4. Create a document that uses heading styles H1, H2, and H3.  Make all three headings appear identical.  (Normally H1 uses a larger font, and H3 a smaller font.)
    5. Create a document with three paragraphs of text.  Make the font color of one paragraph red, one blue, and one black.
    6. Repeat (2), but do it with a style sheet in an external file. If you get stuck, look here for the html and here for the style sheet.

    Last modified on 8 December 2000by abrams@vt.edu.