Now that we've covered the reasons why using CSS is so valuable to a web developer, let's get started with a review of CSS basics.

Documents contain 3 elements:

  1. Content (which is not the focus here)
  2. Valid (and validated) structural markup
  3. Presentation styles

We will focus on separating the structural markup from the presentation styles by using Cascading Style Sheets. There are 3 methods of using CSS with documents; inline, embedded and linked.

Inline Styles

Inline styles apply CSS directly to each individual HTML element on a web page using the STYLE attribute.

<p style="font-family: arial, Helvetica, sans-serif; font-size: 16px; text-align: center; font-weight: bold; color: #CC0000; background-color: #FFFF99;"> Here is an example of a sentence which uses an inline style.</p>

Here is an example of a paragraph which uses an inline style.

Embedded Styles

Embedded styles apply CSS to the current page. It consists of CSS rules placed between a pair of STYLE tags in the head of the HTML page.

  1. The embedded STYLE tag must be in the HEAD of the HTML document.
  2. Styles should be enclosed in HTML comments or CDATA2 tags to prevent them from being interpreted as HTML or XML.
  3. Embedded styles are limited to the page which contains them.
  4. Embedded and linked CSS syntax are the same.
  5. Use embedded styles to test. When complete, add them to the linked CSS document and replace the style tag with the link tag in the HEAD of the document.
  6. Embedded styles take precedence over all styles except inline styles in the cascade of styles.1
<style type="text/css"> /* <![CDATA[ */   h1 { font-family: Impact, Helvetica, sans-serif; font-size: 20px; color: #006600; }   /* ]]> */ </style>

Here is an example of a heading which uses an embedded style.

Linked (External) Styles

Linked styles is the only method which truly separates the presentation from the structural markup. Using the LINK tag in the HEAD of the document, the browser links to a separate CSS document which contains all the styles. Any and all pages in the site can link to this document, reducing and simplifying maintenance for the developer.

<link rel="stylesheet" href="review.css" type="text/css" media="screen" /> <link rel="stylesheet" href="review_print.css" type="text/css" media="print" />

The Lecture pages in this course are examples of documents that use an external, or linked, style sheet.

_______________
1 The "Cascade" of styles will be covered in a future lesson.
2 The use of CDATA tags is covered on p. 32 of the Stylin' book.

References