Lab 3: Cascading Style Sheets

Today you will get practice specifying the style of HTML tags using Cascading Style Sheets (CSS).
In particular, we will use an existing external CSS in our document,and then we will modify it to better fit our needs.

We may also get some practice with document-level and in-line CSS as time permits.

The Cascading Style Sheets lecture notes give an overview of CSS.

The CSS reference page describes details of the syntax, and is a useful guide to the CSS language.

Get started

Create a Lab3 folder on your desktop. You will save every document related to this lab inside folder Lab3.

Save a copy of this document, in the folder Lab3, to use it as a starting point. Name this copy lab3.html. (In your browser, File --> Save As...) View lab3.html in a browser. Notice that lab3.html does not use any CSS at this point.

Task 1: Use an existing External Style Sheet

In this exercise we will use an external style sheet that has been written by someone else. In particular, we will use cs110-main-style.css, which is the main style sheet for the CS110 web site.

You just experienced one of the advantages of using an external style sheet: you can easily re-use existing CSS code (if, of course, it fits your purpose).

Here is how my lab3.html looks like at this point.

Task 2: Modify an External Style Sheet

Here is how my lab3.html looks at this point, and this is the document my_style.css.

 

Task 3: Document-level Style Sheets

If you want to specify the appearance of a certain page only, as opposed to a whole web site, you can use Document-level Style Sheets . For example, we may want to change the behavior of the <P> so that, in a certain page, text between the <P> and </P> shows with a margin around and on an orange background.

To do that, add this piece of CSS code in the HEAD part of your lab3.html document:

<STYLE TYPE="text/css">
 P {margin: 1cm 4cm 1cm 4cm; background-color: orange;}
</STYLE>

Save your document, and view it on a browser to see the results. Notice how this piece of document-level css code affects all the <P> tags in your lab3.html document.

We could also use Document-level CSS code to show an image centered with its caption, by defining a DIV class. Add the following code in the HEAD of your document:

div.ImgCaption {
   width: 300;
   padding: 15px;
   border: 1px solid black;
   text-align: center;
   font-size: small;
}


Then use the defined class in your html document as needed.

Here is how my lab3.html looks at this point.

Task 4: In-line Style Sheets

With in-line cascading sheets you can modify the appearance of a specific tag within a certain document. As an example, let's modify the <BLOCKQUOTE> so that text between the <BLOCKQUOTE> and </BLOCKQUOTE> appears with a certain-look border, and some padding around it. To do that, add the STYLE attribute to the tag, as shown below:

<BLOCKQUOTE STYLE="border: medium double black;
   padding-left: 15px;
   padding-right: 15px;
   padding-top: 15px;
   padding-bottom: 15px;">

 

Often times you want to center an image on the page. You can do it using in-line style sheet code to surround the image with a centered paragraph:

<P STYLE="text-align: center">
  <IMG SRC="weasel.jpg" ALT="weasel image" >
</P>

Here is how my lab3.html looks at this point.

Keep in mind that using CSS this way (in-line), although convenient at times, can result in pages that are difficult to maintain and update.

Introduction | Syllabus | Assignments | Documentation | Project

Computer Science 110
Date Created: January 2006
Date Modified: February 2008