E. Cascading Stylesheets (CSS)
1. Stylesheets give you more control

Stylesheets allow you to have much more fine-grained and concise control over what you are doing. They also should conform closely to standards. In theory, all browsers should interpret the formatting information you put in your stylesheets the same way.

The main advantage of stylesheets is that you can ensure that your design and layout is standard across all pages in a site, and, if you want to change something, you only have to do it once. Stylesheets are essential for large sites and sites with dynamic content.

2. Stylesheets make your pages accessible

Using a stylesheet means that, if a person has different requirments to access your text, they only need to change the stylesheet to completely reformat the display.

3. Stylesheets are a pain?

Unfortunately, there are inconsistencies in how browsers interpret stylesheets. Also, very old browsers will have problems interpreting stylesheets at all. You must perform tests and think about your target audiences to determine how and when to use stylesheets.

Stylesheets make you do more work at the beginning of a project. You don't have the immediacy of diving straight into HTML, and it can be fiddly to get what you want, but it very soon becomes worth it.

4. Ways to define styles

There are three main ways you can define styles for an html document.

The cascading bit of "cascading style sheets" means that there is a heirachy where an inline style can override an internal style, which in turn can overried a externally defined style. The style definition that is closest to the actual thing you want to style is the one that gets used

Inline stylesheet example:

<p style="font-size:10pt; color:teal; font-family:arial"; >Text here will be Arial</p>

<p style="font-size:10pt; color:red; font-family:'comic sans ms'"; >Text here will be comic.  Note the single quotes.  We used them because we had already used double quotes.</p>

And we get...

Text here will be Arial

Text here will be comic. Note the single quotes. We used them because we had already used double quotes.

Internal stylesheet example:

/*an internal style definition goes inside the "head" section of an HTML document*/
<head>
 /*other head elements here*/
<style type="text/css">
div.stop {color:Red;}
p.getReady {color:Yellow;}
h3.go {color:Green; }
</style>

</head>

<div class="stop">Red text in this block</div>
<p class="getReady">Yellow text in this block</div>
<h3 class="go">Green text in this block</div>

</body>
5. External stylesheets

We will focus on external stylesheets, as they are the most powerful and useful in the dynamic environment.

The examples will come from the stylesheet for these pages.

There are two ways to link to an external stylesheet. The

Example: using the link tag

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Stylesheets</title>
  <link rel="stylesheet" type="text/css" href="htmlCourse.css" />

Example: using "import". This method used to be used a lot because Netscape could understand it but could not understand the link tag. It is useful to know because you can use it within other stylesheets.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Stylesheets</title>
    <style type="text/css">
    @import url("htmlCourse.css");
    </style>
</head>
6. Defining Styles

Here is the stylesheet for this set of pages. You see that it defines properties such as the margins of the whole document, and some different sizes and spacings of text. Note that the margins specfied in the Example are relative to the margins already specified for the body of the document.

body {
    font-family:arial, helvetica, sans-serif;
    font-size:14px;
    color: black;
     background-color: white;
     margin-top: 20px;
     margin-right:50px;
     margin-left: 50px;
     margin-bottom: 20px;
      }

p  {font-family:arial, helvetica, sans-serif;     font-size:14px; color:black; text-align:justify; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; }

.SubHeading {font-family:arial, helvetica, sans-serif;     font-size:14px; color:black; font-weight:bold; text-align:justify; padding-top:30px; padding-right:0px; padding-bottom:0px; padding-left:0px; }
    
.Heading {font-family:arial, helvetica, sans-serif;     font-size:16px; color:black; font-weight:bold; text-align:justify; padding-top:10px; padding-right:0px; padding-bottom:10px; padding-left:0px; }

.Example{font-family:monospace, "Courier New"; font-size:14px; color:black; line-height: 1.5; background-color:#c0c0c0; border-style: dotted; border-color: red; margin-top: 20px;  margin-right: 50px;  margin-left: 50px; margin-bottom: 20px; padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px}
    
li {
    font-family:arial, helvetica, sans-serif;
    font-size:14px;
    font-style: italic;
    color: black; }
    
.code {font-family:"Courier", monospace; font-size:14px; color: black}

7. Selecting styles to use in your document

Look at the source code for this document and see how the styles are selected

<div class="myStyle">This block will be styled according to the style you have defined in your stylesheet called .myStyle, or div.myStyle if you have defined your styles to be used only with specific elements.</div>

<span id="uniqueStyle">This block will be styled in a way that you have defined for this element unquely. It will have been defined in the stylesheet as #uniqueStyle</span>
8. Layout with Styles

You can also control the layout of your document with your Stylesheet. Layout with styles is quite badly supported in older browsers, but it is very good to do. It separates the content and the formatting information of your site, making it easier to incorporate dynamic elements and to update a whole site.

You can define the position of each block as either "abolute" or "relative". You can also define an element as "fixed" so that it always appears, in the same place, no matter where the viewer has scrolled down to. This can be useful for navigation elements. You can use styles to position text or images

For more information see Castro p.175 or Shelley p. 185. Example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Altering layout with CSS</title>

<style type="text/css">

#nme  {font-size: 34pt;
            position:absolute;
            top: 80px; left: 100px;
            color:black; }

#and   {font-size: 44pt;
            position:relative;
            top:50px; left: 170px;
            color:blue; }

#idm   {font-size: 24pt;
            position:absolute;
            top: 100px; left: 50px;
            color:red; }

#know {font-size: 24pt;
            position:absolute;
            top: 50px; left: 70px;
            color:green; }

</style>
</head>

<body>
<div id="nme">NME Students</div>
<div id="and">and</div>
<div id="idm">IDM Students</div>
<div id="know">know CSS very well</div>
</body>
<html>

See this lovely layout. Cut and paste the code into your own text editor and play with it.

Old browsers that do not understand your stylesheet will simply display the blocks in the order that they appear. Therefore a good tip is to make sure that you type your blocks in an order that would still make sense even if the positioning elements were not understood.

Further Reading and references
Relevant sections in Castro and Shelley
CSS reference on Castro's site: http://www.cookwood.com/html/extras/cssref.html
http://www.w3schools.com/css/default.asp

Next: Project