User Tools

Site Tools


eg-259:xhtml

~~SLIDESHOW~~

We recommend that you view this document in slideshow mode first then go back and read the notes.

The Structural Layer

Origins and Evolution of HTML

Reasons to use the XHTML syntax rather than HTML

  • HTML has lax syntax rules, leading to sloppy and sometimes ambiguous documents
  • HTML processors do not even enforce the few syntax rule that do exist in HTML
  • XHTML syntax is stricter, leading to clean and clear documents in a standard form
  • The syntactic correctness of XHTML documents can be validated
  • HTML5 allows both the less strict conventions of HTML5 and the stricter XHTML syntax.
  • To avoid relearning what you already know, we will continue to use the XHTML syntax except where HTML5 syntax is simpler to understand.

Basic Syntax

  • Elements are defined by tags (markers) which act as containers for content: e.g. <name> content </name>.
  • The opening tag and its closing tag together specify a container for the content they enclose
  • Not all elements have content: if a tag has no content, its form is <name />
  • The container and its content together are called an element
  • If a tag has attributes, they appear between its name and the right bracket of the opening tag: e.g <element [name=“value”]*> content </element>
  • Comment form: <!- … –>

Notes

  • Tag format:
    • Opening tag: <name>
    • Closing tag: </name>
  • Attributes:
    • in the example [name=“value”]* means that an attribute – defined as a name followed by = followed by a quoted value – can appear 0 or more times in an element's opening tag. Thus attributes are optional, but when they appear they must have the form name=“value”. Furthermore if there are attributes, there can be any number of attributes.
  • Browsers ignore comments, unrecognizable tags, line breaks, multiple spaces, and tabs
  • Even if they are recognized by the browser, tags are only suggestions to the browser.

Standard HTML5 Document Structure

  • Every HTML5 document begins with the simple DOCTYPE declaration:
      <!DOCTYPE html>
  • The elements <html>, <head>, <title>, and <body> are required in every document
  • The whole document must have <html> as its root
  • For compatibility with XHTML specifications the html element may use the xmlns attribute:
      <html lang="en">

The language of the document is declared in the lang attribute. This is useful for browsers who may be visiting your site from another non-english speaking country (particular if that country uses a different character set).

A Minimal XHTML Document

  • A document consists of a head and a body
  • The <title> element is used to give the document a title
  • A <meta> element that specifies the character encoding used for the document should be included
  • Text is normally placed in paragraph elements (<p>) in the body of the document
  • W3C HTML Validation Service http://validator.w3.org/file-upload.html

Notes

  • The content of the <title> element is normally displayed in the browser's window title bar (at the top of the display).
  • Prior to XHTML 1.1, a document could have either a body or a frameset
  • A common setting for the <meta> tag is utf-8 defined like this
<meta charset="utf-8" />
  • Paragraph Elements
    • The <p> tag breaks the current line and inserts a blank line – the new line gets the beginning of the content of the paragraph
    • The browser puts as many words of the paragraph's content as will fit in each line

An Example Minimal Valid Document

first document
<!DOCTYPE html>
<!-- example.html: A trivial document -->
<html lang="en">
  <head> 
    <meta charset="utf-8" />
    <title> Our first document </title>
  </head>
  <body>
    <p> 
      Greetings from your Webmaster!
    </p>
  </body>
</html>
  • I recommend that you download a copy of this document (example.html) and use it as a template.

[ Jump to CSS ]

Day-to-day Markup

Line Breaks

  • The effect of the <br /> tag is the same as that of <p>, except for the blank line
  • An example of paragraphs and line breaks:
      <p>On the plains of hesitation</p> <p> bleach the
      bones of countless millions  <br />
      who, at the dawn of victory <br /> sat down
      to wait, and waiting, died.</p>

Notes and Details of the Example

  • Note that <br /> has no closing tag!
  • The example of paragraphs and line breaks: line-breaks.html
example of paragraphs and line breaks
<!DOCTYPE html>
<!-- line-breaks.html
     An Example of Paragraphs and Line Breaks 
-->
<html lang="en">
  <head> 
      <meta charset="utf-8" />
      <title> Paragraphs and Line Breaks </title>
  </head>
  <body>
     <p>On the plains of hesitation</p> <p> bleach the
      bones of countless millions  <br />
      who, at the dawn of victory <br /> sat down
      to wait, and waiting, died.</p>
   </body>
</html>
  • Typical display:

<html> <p>On the plains of hesitation</p> <p> bleach the bones of countless millions <br /> who, at the dawn of victory <br /> sat down to wait, and waiting, died.</p> </html>

Headings

  • Six sizes, 1—6, specified with <h1> to <h6>
  • 1, 2, and 3 use font sizes that are larger than the default font size
  • 4 uses the default size
  • 5 and 6 use smaller font sizes
  • An example of headings: headings.html

  • Code for the Example
example to illustrate headings
<!DOCTYPE html>
<!-- headings.html
     An example to illustrate headings
-->
<html lang="en">
  <head> 
    <meta charset="utf-8" />
    <title> Headings </title>
  </head>
  <body>
    <h1> Aidan's Airplanes (h1) </h1>
    <h2> The best in used airplanes (h2) </h2>
    <h3> "We've got them by the hangarful" (h3)
    </h3>
    <h4> We're the guys to see for a good used
         airplane (h4) </h4>
    <h5> We offer great prices on great planes
         (h5) </h5>
    <h6> No returns, no guarantees, no refunds,
         all sales are final (h6) </h6>
  </body>
</html>
  • Typical Display

An example of the use of the headings elements

Quotations

  • Content of <blockquote>
  • To set a block of text off from the normal flow and appearance of text
  • Browsers often indent, and sometimes italicize
  • Example: blockquote.html

Code for the Example

example to illustrate a blockquote
<!DOCTYPE html>
<!-- blockquote.html
An example to illustrate a blockquote
-->
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title> Blockquotes </title>
  </head>
  <body>
    <p>
      Abraham Lincoln is generally regarded as one of the greatest
      presidents of the U.S. His most famous speech was delivered
      in Gettysburg, Pennsylvania, during the Civil War. This
      speech began with
    </p>
    <blockquote>
      <p>
        "Fourscore and seven years ago our fathers brought forth on
        this continent, a new nation, conceived in Liberty, and
        dedicated to the proposition that all men are created equal.
      </p>
      <p>
        Now we are engaged in a great civil war, testing whether
        that nation or any nation so conceived and so dedicated,
        can long endure."
      </p>
    </blockquote>
    <p>
      Whatever one's opinion of Lincoln, no one can deny the
      enormous and lasting effect he had on the U.S.
    </p>
  </body>
</html>

Typical Display

An example of the use of the blockquote element

Font Styles and Sizes

<small> The </small> <big> sleet <big> in <big> <i> Crete
</i><br /> lies </big> <b> completely </b> </big>
in </big> the <tt>street</tt>

<html> <small> The </small> <big> sleet <big> in <big> <i> Crete </i><br /> lies </big> <b> completely </b> </big> in </big> the <tt>street</tt> </html>


  • Can be nested
    • Boldface – <b>
    • Italics – <i>
    • Larger – <big>
    • Smaller – <small>
    • Monospace – <tt>

These tags are not affected if they appear in the content of a <blockquote>, unless there is a conflict (e.g., italics)

Subscripts and Superscripts

  • Subscripts with sub
  • Superscripts with sup

<html> x23 </html>

x<sub>2</sub><sup>3</sup>

Very Important Note

  • All of this font size and font style stuff can (and should) be done with style sheets, but the tags are not yet deprecated
  • Block elements CANNOT be nested in in-line elements

Character Entities

Needed to enter certain special characters into a web page

Char Entity Meaning
& &amp; Ampersand
< &lt; Less than
> &gt; Greater than
&quot; Double quote
' &apos; Single quote
¼ &frac14; One quarter
½ &frac12; One half
¾ &frac34; Three quarters
? &deg; Degree
(space) &nbsp; Non-breaking space

Notes:

The first four character entities are needed to display the characters that are used by XHTML elements and attributes. For example to display <p class=“greeting”>Hello World!</p> in a web page we have to enter &lt;p class=&quot;greeting&quot;&gt;Hello World!&lt;/p&gt; and to display the & symbol we have to use &amp;amp;. This can make the display of HTML code in a web page quite a challenge!

For a full list of the 252 usable character entities see 1 and 2.

Other Elements

Horizontal Rules and Meta Tags

  • Horizontal rules
    • <hr /> draws a line across the display, after a line break
  • The meta element (for search engines)
    • Used to provide additional information about a document, with attributes, not content

Images

  • GIF (Graphic Interchange Format)
  • JPEG (Joint Photographic Experts Group)
  • Both use compression, but JPEG compression is better
  • Portable Network Graphics (PNG)

  • GIF – 8-bit color (256 different colors)
  • JPEG – 24-bit color (16 million different colors)
    • Both use compression, but JPEG compression is better
  • PNG
    • Relatively new
    • Should eventually replace both GIF and JPEG

Images – Markup

<img src = "comets.jpg"
     alt = "Picture of comets" />
  • Images are inserted into a document with the <img /> tag with the src attribute
  • The alt attribute is required by XHTML
  • The <img> tag has 30 different attributes, including width and height
  • Example Document: image.html

  • Purpose of the alt attribute:
    1. Non-graphical browsers
    2. Browsers with images turned off
    3. Accessibility, e.g. screen readers

Code for the Example

example to illustrate an image
<!DOCTYPE html>
 
<!-- image.html
     An example to illustrate an image
     -->
<html lang="en">
  <head> 
      <meta charset="utf-8" />
      <title> Images </title>
  </head>
  <body>
    <h1> Aidan's Airplanes </h1>
    <h2> The best in used airplanes </h2>
    <h3> "We've got them by the hangarful" </h3>
    <h2> Special of the month </h2>
    <p>
      1960 Cessna 210 <br />
      577 hours since major engine overhaul<br />
      1022 hours since prop overhaul <br /><br />
<img src = "c210new.jpg"  alt = "Picture of a Cessna 210" />
      <br />
      Buy this fine airplane today at a remarkably low price
      <br />
      Call 999-555-1111 today!
    </p>
  </body>
</html>

Typical Display

An example of a document with an image

  • Hypertext is the essence of the Web!
  • A link is specified with the href (hypertext reference) attribute of <a> (the anchor tag)
  • The content of <a> is the visual link in the document
  • If the target is a whole document (not the one in which the link appears), the target need not be specified in the target document as being the target
  • Example: link.html

Notes

Relative addressing of targets is easier to maintain and more portable than absolute addressing

Code for the Example

example to illustrate a link
<!DOCTYPE html>
 
<!-- link.html
     An example to illustrate a link
     -->
<html lang="en">
  <head> 
      <meta charset="utf-8" />
      <title> A link </title>
  </head>
  <body>
    <h1> Aidan's Airplanes </h1>
    <h2> The best in used airplanes </h2>
    <h3> "We've got them by the hangarful" </h3>
    <h2> Special of the month </h2>
    <p>
      1960 Cessna 210 <br />
      <a href = "C210data.html"> Information on the Cessna 210 </a>
    </p>
  </body>
</html>

Typical Display

An example of a document with links

Link Target

The Link Target is C210data.html

example to illustrate the target of a link
<!DOCTYPE html>
 
<!-- C210data.html
     An example to illustrate the target of a link
     -->
<html lang="en">
  <head> 
      <meta charset="utf-8" />
      <title> 1960 Cessna C210 </title>
  </head>
  <body>
    <h1> 1960 Cessna 210 </h2>
    <p>
      577 hours since major engine overhaul<br />
      1022 hours since prop overhaul <br /><br />
      <img src = "c210new.jpg"  alt = "Picture of a Cessna 210" />
      <br />
      Buy this fine airplane today at a remarkably low price
      <br />
      Call 999-555-1111 today!
    </p>
  </body>
</html>

Typical Display

The Target of the Link Example

  • If the target is not at the beginning of the document, the target spot must be marked
  • Target labels can be defined in many different tags with the id attribute, as in:
      <h1 id = "baskets"> Baskets </h1>
  • The link to an id must be preceded by a ”pound sign“ (#); If the id is in the same document, this target could be:
      <a href = "#baskets"> What about baskets? </a>
  • If the target is in a different document, the document reference must be included:
      <a href = "myAd.html#baskets"> Baskets </a>

  • Style note: a link should blend in with the surrounding text, so reading it without taking the link should not be made less pleasant
  • Links can have images:
       <a href = "c210data.html">
       <img src = "smallplane.jpg"
         alt = "Small picture of an airplane " />
           Info on C210 </a>

Exercises to Demonstrate Basic XHTML

  1. Create, test and validate an XHTML document for yourself. including your name, address, and e-mail address. You must include your student number, course and level. This document must use several headings and <big>, <small>, <hr />, <p> and <br /> tags.
  2. Add pictures of yourself and at least one image (e.g. of your friend, spouse, pet) to the document created in Exercise 1.
  3. Add a second document to the document created for Exercise 1 that describes part of your background, using the word background as the link content. This document should have a few paragraphs of your personal history.

More Advanced Markup

Unordered Lists

  • The list is the content of the <ul> tag
  • List elements are the content of the <li> tag: unordered.html

example to illustrate an unordered list
<!DOCTYPE html>
 
<!-- unordered.html
     An example to illustrate an unordered list
     -->
<html lang="en">
  <head> 
      <meta charset="utf-8" />
      <title> Unordered list </title>
  </head>
  <body>
    <h3> Some Common Single-Engine Aircraft </h3>
    <ul> 
      <li> Cessna Skyhawk </li>
      <li> Beechcraft Bonanza </li>
      <li> Piper Cherokee </li>
    </ul>
  </body>
</html>

Ordered lists

  • The list is the content of the <ol> tag
  • Each item in the display is preceded by a sequence value: ordered.html

example to illustrate an ordered list
<!DOCTYPE html>
 
<!-- ordered.html
     An example to illustrate an ordered list
     -->
<html lang="en">
  <head> 
      <meta charset="ut-8f" />
      <title> Ordered list </title>
  </head>
  <body>
    <h3> Cessna 210 Engine Starting Instructions </h3>
    <ol>
      <li> Set mixture to rich </li>
      <li> Set propeller to high RPM </li>
      <li> Set ignition switch to "BOTH" </li>
      <li> Set auxiliary fuel pump switch to "LOW PRIME" </li>
      <li> When fuel pressure reaches 2 to 2.5 PSI, push 
           starter button
      </li>
    </ol>
  </body>
</html>

Nested lists

  • Any type list can be nested inside any type list
  • The nested list must be in a list item: nested_lists.html

example to illustrate nested lists
<!DOCTYPE html>
<!-- nested_lists.html
An example to illustrate nested lists
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title> Nested lists </title>
  </head>
  <body>
    <h3> Aircraft Types </h3>
    <ol>
      <li>
        General Aviation (piston-driven engines)
        <ol>
          <li>
            Single-Engine Aircraft
            <ol>
              <li>
                Tail wheel
              </li>
              <li>
                Tricycle
              </li>
            </ol>
            <br />
          </li>
          <li>
            Dual-Engine Aircraft
            <ol>
              <li>
                Wing-mounted engines
              </li>
              <li>
                Push-pull fuselage-mounted engines
              </li>
            </ol>
          </li>
        </ol>
        <br />
      </li>
      <li>
        Commercial Aviation (jet engines)
        <ol>
          <li>
            Dual-Engine
            <ol>
              <li>
                Wing-mounted engines
              </li>
              <li>
                Fuselage-mounted engines
              </li>
            </ol>
            <br />
          </li>
          <li>
            Tri-Engine
            <ol>
              <li>
                Third engine in vertical stabilizer
              </li>
              <li>
                Third engine in fuselage
              </li>
            </ol>
          </li>
        </ol>
      </li>
    </ol>
  </body>
</html>

Definition lists

  • For glossaries, etc.
  • List is the content of the <dl> tag
  • Terms being defined are the content of the <dt> tag
  • The definitions themselves are the content of the <dd> tag: definition.html

example to illustrate definition lists
<!DOCTYPE html> 
<!-- definition.html
An example to illustrate definition lists
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title> Definition lists </title>
  </head>
  <body>
    <h3> Single-Engine Cessna Airplanes </h3>
    <dl>
      <dt>
        152
      </dt>
      <dd>
        Two-place trainer
      </dd>
      <dt>
        172
      </dt>
      <dd>
        Smaller four-place airplane
      </dd>
      <dt>
        182
      </dt>
      <dd>
        Larger four-place airplane
      </dd>
      <dt>
        210
      </dt>
      <dd>
        Six-place airplane - high performance
      </dd>
    </dl>
  </body>
</html>

Exercises to Demonstrate Lists

  1. Create, test and validate an XHTML document to describe an unordered list of a typical supermarket shopping list you write. (If you've never made such a list, use your imagination).
  2. Create, test and validate an XHTML document to describe an unordered list of at least 4 countries. Each element of the list must have a nested list of at least three cities in the country.
  3. Create, test and validate an XHTML document to describe an ordered list of your five favourite movies.
  4. Modify the list of Exercise 6 to add nested, unordered lists of at least two actors and/or actresses in each of your favourite movies.

Tables

  • A table is a matrix of cells, each possibly having content
    • The cells can include almost any element
    • Some cells have row or column labels and some have data
  • A table is specified as the content of a <table> tag
  • A border attribute in the <table> tag specifies a border between the cells
  • Tables are given titles with the <caption> tag, which can immediately follow <table>

Notes

  • If border is set to “border”, the browser's default-width border is used
  • The border attribute can be set to a number, which will be the border width
  • Without the border attribute, the table will have no lines!

Table Rows and Cells

  • Each row of a table is specified as the content of <tr> tag
  • The *row headings* are specified as the content of a <th> tag
  • The contents of a *data cell* is specified as the content of a <td> tag
  • An example table: table.html

example of a simple table
<!DOCTYPE html>
<!-- table.html
An example of a simple table
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title> A simple table </title>
  </head>
  <body>
    <table border="border">
      <caption>
        Fruit Juice Drinks
      </caption>
      <tr>
        <th></th>
        <th> Apple </th>
        <th> Orange </th>
        <th> Screwdriver </th>
      </tr>
      <tr>
        <th> Breakfast </th>
        <td> 0 </td>
        <td> 1 </td>
        <td> 0 </td>
      </tr>
      <tr>
        <th> Lunch </th>
        <td> 1 </td>
        <td> 0 </td>
        <td> 0 </td>
      </tr>
      <tr>
        <th> Dinner </th>
        <td> 0 </td>
        <td> 0 </td>
        <td> 1 </td>
      </tr>
    </table>
  </body>
</html>

More Table Elements and Attributes

For information on column and row spanning, cell alignment, cell spacing and padding see the notes.


Tables – colspan

  • A table can have two levels of column labels, e.g.
      <table border="border">
      <tr>
        <th colspan = "3"> Fruit Juice Drinks </th>
      </tr>
      <tr>
        <th> Orange </th>
        <th> Apple </th>
        <th> Screwdriver </th>
      </tr>
      </table>
  • If so, the colspan attribute must be set in the <th> tag to specify that the label must span some number of columns:
      <table border="border">
        <tr>
          <th colspan = "3"> Fruit Juice Drinks </th>
        </tr>
        <tr>
          <th> Apple </th>
          :
        </tr>
        :
      </table>

Tables – rowspan

  • If the rows have labels and there is a spanning column label, the upper left corner must be made larger, using rowspan, e.g.
        <table border = "border">
          <tr>
            <td rowspan = "2"> &nbsp; </td>
            <th colspan = "3"> Fruit Juice Drinks
            </th>
          </tr>
          <tr>
            <th> Apple </th>
            <th> Orange </th>
            <th> Screwdriver </th>
          </tr>
        </table>
  • Is created with:
        <table border = "border">
        <tr>
          <td rowspan = "2"> &nbsp; </td>
          <th colspan = "3"> Fruit Juice Drinks
          </th>
        </tr>
        <tr>
          <th> Apple </th>
          :
        </tr>
          :
      </table>
  • Download the colspan/rowspan example: cell_span.html. Here is the complete source code:
<!DOCTYPE html|An example to illustrate rowspan and colspan>
<!-- cell_span.html
An example to illustrate rowspan and colspan
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title> Rowspan and colspan </title>
  </head>
  <body>
    <table border = "border">
      <caption>
        Fruit Juice Drinks and Meals
      </caption>
      <tr>
        <td rowspan = "2"> &nbsp; </td>
        <th colspan = "3"> Fruit Juice Drinks </th>
      </tr>
      <tr>
        <th> Apple </th>
        <th> Orange </th>
        <th> Screwdriver </th>
      </tr>
      <tr>
        <th> Breakfast </th>
        <td> 0 </td>
        <td> 1 </td>
        <td> 0 </td>
      </tr>
      <tr>
        <th> Lunch </th>
        <td> 1 </td>
        <td> 0 </td>
        <td> 0 </td>
      </tr>
      <tr>
        <th> Dinner </th>
        <td> 0 </td>
        <td> 0 </td>
        <td> 1 </td>
      </tr>
    </table>
  </body>
</html>

Table Sections

  • Header, body, and footer, which are the elements: thead, tbody, and tfoot
  • If a document has multiple tbody elements, they are separated by thicker horizontal lines

Exercises to Demonstrate Tables

  1. Create, test and validate an XHTML document to describe a table with the following contents:
    • The columns of the table must have the headings Pine, Maple, Oak and Fir
    • The rows must have labels Average Height, Average Diameter, Typical Lifespan, and Leaf Type.
    • You can make up the data cell values.
  2. Modify, test and validate an XHTML document from Exercise 1 to add a second level column label, Tree, and a second-level row label, Characteristics.
  3. Create, test and validate an XHTML document to describe a table with columns for country, national language, capital city and population. There must be at least five countries in the table.

Frames

  • Frames are rectangular sections of the display window, each of which can display a different document
  • Because frames are no longer part of HTML5, you cannot validate a document that includes frames
  • For this reason we shall say no more about them in this module!

Syntactic Differences between HTML & XHTML

  • Case sensitivity
  • Closing tags
  • Quoted attribute values
  • Explicit attribute values
  • id and name attributes

The Structural and Presentation Layers

eg-259/xhtml.txt · Last modified: 2012/02/01 20:18 by eechris