Introduction to CSS (Cascading Style Sheets)

Created by Cameron Jones, minor modifications by Ingbert Floyd

Click here to see the styled version.

Table of Contents
  1. What are CSS?
  2. Why use CSS?
  3. Why everything isn't CSS
  4. Gentle Introduction
  5. Media Types
  6. HTML Trees
  7. Box Model
  8. Selectors
  9. Properties
  10. Activity
  11. More Info
  12. Submission
  13. Grading

What are Cascading Style Sheets?

Cascading style sheets (CSS) are a mechanism by which web authors can define the ways in which their web content is rendered on the client's machine (browser). HTML by default offers only a single presentation of web content - which is not necessarily the 'right' one, nor the one you want to use for your website. Attempts to work around the limitations of the default HTML presentation are often bulky, limited and insufficient. CSS is a much more 'elegant' way of redefining the presentation of web content without having to reauthor the HTML web content.

Why use CSS?

Why isn't everything on the web CSS?

Introduction to CSS

Media Types

CSS also allows you to define particular styles based on the media of the client (type of display). So, people on mobile phones, blind people, etc. can download different stylesheets for your page to present them in different ways. Here are the media types defined by W3C:

Can define styles for particular media, or import entire stylesheets depending on the media.


@all h2
{
	//style defined for all media
}

@screen p
{
	//paragraph for screen media
}

@aural img
{
	//style for how to present images to a screen reader
}

//import the default css for screen presentation
@import url(default.css) screen;

//a braille reader should use the braille css
@import url(braille.css) braille;

//mobile device users should get a stripped down css.
@import url(mobile.css) handheld;

Document Tree Model or Why Cascading Style Sheets are "Cascading"

HTML documents have a hierarchical tree structure. HTML tags are nested within other tags, forming a hierarchical structure of the tags. For example, this HTML document (from W3C)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
  <TITLE>My home page</TITLE>
  <BODY>
    <H1>My home page</H1>

    <P>Welcome to my home page! Let me tell you about my favorite composers:</P>
    <UL>
      <LI> Elvis Costello </LI>
      <LI> Johannes Brahms </LI>
      <LI> Georges Brassens </LI>
    </UL>

  </BODY>
</HTML>

Has a tree-structure that looks like this:

Tree structure for sample HTML

The CSS Box Model

Before going any further, we need to understand what is happening with these styles and elements. CSS utilizes a box model to represent every element in an HTML document. Each element is represented as a box containing content. The boxes can be strung together either in inline or in block layout to construct the HTML rendering.

Here is what a box looks like, conceptually (From W3C):

CSS Box Model Diagram

We can control all of these properties with stylesheets, to customize the look and feel of each element in the HTML document. Every tag has these properties. For more information on the box model check out the following links:

Selectors

CSS has a lot of power and flexibility in allowing you to select which elements you want styles to apply to. You can select elements by their name (h1, h2, p, etc), their class (based on a value you put in the class attribute), a combination of the two, their id, their hierarchical position, and many others. Here are some basic examples using this document as source.

//select all divs AND paragraphs
div, p
   ^ comma

//select only the divs which are of class 'section'
div.section
   ^ period

//select paragraphs inside of divs
div p
   ^ space

//select images inside of divs
div img

//select divs inside of section-divs
div.section div

//select anchors
a

//select links
a:link

//select links when they are moused over
a:hover

//select active (clicked) links
a:active

//select visited links
a:visited

CSS Properties

Some common properties you can control in CSS include:

Property Description Values
color The foreground color aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow or hex value (#rrggbb, #rgb, rgb(x,x,x))
background The background color Color value or image URL
font-family The font style to be used Font group name, eg. 'arial, helvetica, sans-serif'
font-weight Boldness 'normal', 'bold', 'bolder', 'lighter'
border Border width, style and color Width (usually in pixels), style (solid, dashed, dotted), color value
margin The margin around the element (space between it and another element) Margin width
padding The padding inside the element (space between content and the edge of the element) Padding width

Playtime

Look at some of the following sites. Check out their stylesheets. Pick a page of your own and save it to your computer. Open the stylesheet and start tinkering.

In your browser, select "Save Page As" option and download the complete page (including images and CSS) open up the saved page in your browser and open the css in a text editor. Start making changes to see how they effect the output. Try deleting everything in the css (save a backup first). What does the page look like without any styles?

For More Info

Submission

This Lab should be submitted to your GSLIS I: drive. Please create a new folder in your courseweb_html directory, and place all the files of the lab in this new folder.

To submit your Lab, upload 4 versions of the HTML + stylesheet of the page you chose to edit. The HTML should be identical on each version. The stylesheets should be the only thing that change. Each version should have a significantly different look and feel. Changing the style of one header is not sufficient, for example. One version of the page must have a blank stylesheet. Do not try to validate these 4 versions of the page you have downloaded unless you have time to spare, and want to get better at your HTML. I strongly suggest that you put each version of the HTML in separate folders (directories). This will help you keep track of the different versions, and which files belong with which version.

Then submit a brief writeup as an HTML document in the standard form required for this class. In the writeup, explain what you did with each of the 4 versions, what you learned from the changes you made to each version, etc. This can be either in essay or in bullet point form, but I require enough explanation so that you demonstrate what you have learned. Please provide links in your writeup to each version of the page you saved. The HTML for your writeup must validate against XHTML 1.0.

Because some students have been pushing the line as far as what they write for their answers, there must be 100-250 words devoted to each version of the page. Thus, the total writeup must be at least 400 words.

Grading

Out of 25 points total: