XHTML for CSS

For the purposes of this class, effective use of CSS requires:

Separation of content and presentation

Old forms of HTML mixed in many element attributes to set font styles, font sizes, font colors, background colors, padding, and much more to control how pages looked. Combined with the use of tables to control layouts, old-style HTML got very complex and hard to update.

With XHTML and CSS, content is maintained in the XHTML and all the presentation is controlled by the CSS, making it much easier to maintain each independent of the other.

Proper XHTML

Consider the following simple XHTML file:

          
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html> 
      <head> 
        <title>Moby Dick, or, The Whale</title> 
        <link rel="stylesheet" href="melville.css" type="text/css" />
      </head> 
      <body> 
        <h1>Moby Dick, or, The Whale</h1> 
        <p>Call me Ishmael.</p> 
      </body> 
    </html>
    

The XHTML has the following characteristics:

DOCTYPE statements

The DOCTYPE statement declares what type of XHTML is being used on the page. Browsers use the DOCTYPE statement to decide how to process a page. Pages with a complete DOCTYPE statement and correct XHTML are processed in "standards" mode, which ensures the page will display in the most consistent, predictable way across a variety of browsers. Pages without a DOCTYPE statement or with incorrect XHTML will be processed in "quirks" mode and are less likely to show in the way their author intended.

Divs and Spans

Two types of elements that were seldomly used in old-style HTML are very important when working with XHTML and CSS: Divs and spans.

Divs enclose a set of XHTML elements in the body, creating a unit of content that can have styles applied to it as a unit, including positioning.

Spans enclose text within an element, such as some text in a paragraph, allowing the span and its contents to have styles applied to it independent of whatever styles apply to the element that contains it.

        
    <div style="text-align: center;">
      <h2>Moby Dick, or, The Whale</h1> 
      <p>This story of a obsessive sea captain's quest to 
        <span style="color: red;">find and destroy<span> 
        an extraordinary whale that 
        had taken his leg in a previous encounter is sometimes
        seen as an allegory for all of humanity's obsessive pursuits.
      </p>
    </div> 
         

Example