Advanced HTML

Tables

Tables make it possible to format text, data, images, or some combination in such a way as to align them in a sensible manner. An HTML table consists of several container tags: <table>, <tr>, <td>, and <th>.

<table>

This tag is the wrapper that surrounds the other tags that make up the table. The <table> tag is necessary in every table; without it or the ending </table> all the interior tags will be ignored.

<tr>

The Table Row tag, <tr>..</tr>, denotes the beginning of a row of the table. The </tr> tag must be used to end each row. The total number of <tr> tags specifies the number of rows contained within a table.

<td>

The Table Data tag, <td>..</td>, contains the actual data of the table. The <td> tag may only appear within a table row, <tr>, tag. Each data tag is called a cell and it is not necessary to have the same number of cells in each row -- the short rows will be filled with blank cells.

<th>

The Table Header tag, <th>..</th>, acts the same as the <td> tag except that, by default, all data within the cell is aligned to the center and is boldface.

An example of a simple table:

<table>
  <tr>
    <th>Name</th>
    <th>BA</th>
    <th>HR</th>
  </tr>
  <tr>
    <td>Larkin</td>
    <td>.300</td>
    <td>3</td>
  </tr>
  <tr>
    <td>Morris</td>
    <td>.290</td>
    <td>6</td>
  </tr>
</table>
becomes
Name BA HR
Larkin .300 3
Morris .290 6
As with most other tags, it is possible to change the appearance of a table through the use of attributes. Each table tag has its own attributes with some attributes having the ability to be used in more than one tag. The following is a description of each attribute and the table tags it can be used in.

The border attribute can only be used with the table tag. It controls the size of the border surrounding the table. The size is represented by a pixel value with the default being 0, no border.

<table border="2">
  <tr>
    <th>Name</th>
    <th>BA</th>
    <th>HR</th>
  </tr>
  <tr>
    <td>Larkin</td>
    <td>.300</td>
    <td>3</td>
  </tr>
  <tr>
    <td>Morris</td>
    <td>.290</td>
    <td>6</td>
  </tr>
</table>
becomes
Name BA HR
Larkin .300 3
Morris .290 6
The cellpadding and cellspacing attributes can only be used with the table tag. They control the space between table elements. The cellpadding attribute controls the amount of space between cell data and the cell wall. The cellspacing attribute controls the space between the cells of a table. The size is represented by a pixel value.
<table border="2" cellpadding="20" cellspacing="5">
  <tr>
    <th>Name</th>
    <th>BA</th>
    <th>HR</th>
  </tr>
  <tr>
    <td>Larkin</td>
    <td>.300</td>
    <td>3</td>
  </tr>
  <tr>
    <td>Morris</td>
    <td>.290</td>
    <td>6</td>
  </tr>
</table>
becomes
Name BA HR
Larkin .300 3
Morris .290 6
The align attribute can be used on all of the table tags. It controls the alignment of text to the left, center, or right inside a cell, or to align the table on the page. Valid values for this attribute are align="right", align="center", and align="left". When used in a <table> tag, the entire table is affected. If used in the <tr> tag, all cells in that specific row will be aligned accordingly. If used within either the <th> or <td> tag, only that cell will be aligned.
<table border="2">  
  <tr>
    <th>Player Name</th>
    <th>Batting Average</th>
    <th>Home Runs</th>
  </tr>
  <tr align="center">
    <td>Larkin</td>
    <td>.300</td>
    <td align="right">3</td>
  </tr>
  <tr>
    <td align="center">Morris</td>
    <td>.290</td>
    <td>6</td>
  </tr>
</table>
Player Name Batting Avg. Home Runs
Larkin .300 3
Morris .290 6
The valign attribute can be used on all of the tags. It controls the vertical alignment of the text within a cell. Valid values for this attribute are valign="top", valign="middle", and valign="bottom". As with align, what cells the attribute affects is based on what tag it is used in. If the attribute is used in the <table> tag, all cells in the table are aligned accordingly. If used in the <tr> tag, all cells in that specific row will be aligned accordingly. If used within either the <th> or <td> tag, only that cell will be aligned.
<table border="2">  
  <tr>
    <th>Player Name</th>
    <th>Batting Average</th>
    <th>Home Runs</th>
  </tr>
  <tr>
    <td>Barry<br>Larkin</td>
    <td valign="top">.300</td>
    <td>3</td>
  </tr>
  <tr>
    <td>Hal<br>Morris</td>
    <td valign="bottom">.290</td>
    <td valign="middle">6</td>
  </tr>
</table>
Player Name Batting Avg. Home Runs
Barry
Larkin
.300 3
Hal
Morris
.290 6
The bgcolor attribute can be applied to all of the table tags. This attribute controls the background color of the cells. The color can be represented by either the actual color name (i.e. "red") or its hexadecimal value (i.e. #FF0000 = red). If the attribute is used in the <table> tag, all cells in the table are colored accordingly. If used in the <tr> tag, all cells in that specific row will be colored accordingly. If used within either the <th> or <td> tag, only that cell will be colored.
<table bgcolor="white" border="2">  
  <tr bgcolor="#FF0000">
    <th>Player Name</th>
    <th>Batting Average</th>
    <th>Home Runs</th>
  </tr>
  <tr>
    <td>Barry<br>Larkin</td>
    <td>.300</td>
    <td bgcolor="#00FF00">3</td>
  </tr>
  <tr>
    <td>Hal<br>Morris</td>
    <td>.290</td>
    <td bgcolor="0000FF">6</td>
  </tr>
</table>
Player Name Batting Avg. Home Runs
Barry
Larkin
.300 3
Hal
Morris
.290 6
The width and height attributes can be applied to the <table>, <td>, and <th> tags. They control the size of the table elements. The sizes are represented as either pixel sizes or as a relative percentage. If the attribute is used in the <table> tag, the width and height control the size of the entire table. A percentage, in this case, is relative to the width and height of the entire web page.
<table border="2" width="35%">  
  <tr>
    <th>Name</th>
    <th>BA</th>
    <th>HR</th>
  </tr>
  <tr>
    <td>Larkin</td>
    <td width="50">.300</td>
    <td>3</td>
  </tr>
  <tr>
    <td>Morris</td>
    <td>.290</td>
    <td height="50">6</td>
  </tr>
</table>
Name BA HR
Larkin .300 3
Morris .290 6
The colspan and rowspan attributes apply only to the <td> and <th> tags. They control the number of columns and rows (respectively) that a single cell spans. The value represents the number of columns or rows the cell should span. The results are the same whether this attribute is used in either tag.
<table border="2" cellpadding="3">
  <tr>
    <td colspan="4" bgcolor="red">Cincinnati Reds Stats</td>
  </tr>
  <tr>
    <th>Name</th>
    <th>BA</th>
    <th>HR</th>
    <th>RBI</th>
  </tr>
  <tr>
    <td>Larkin</td>
    <td>.300</td>
    <td>3</td>
    <td rowspan="2">40</td>
  </tr>
  <tr>
    <td>Morris</td
    ><td>.290</td>
    <td>6</td>
  </tr>
</table>
becomes
Cincinnati Reds Stats
Name BA HR RBI
Larkin .300 3 40
Morris .290 6

Frames

An HTML frame is an advanced formatting technique that allows a web page to be split up into sub-areas that each hold a separate HTML document. Each sub-area, or frame, retains the full functionality of a non-frame HTML document with the added ability to interact with the other frames. Here is an example of a framed page.

Structure of a frame

Every framed HTML page begins with a frame document, that tells the browser exactly how to load each frame and where to place it on the screen. A frame document is exactly the same as a normal web page except that the <body> tag is replaced by the <frameset> tag. Let's examine, line-by-line, the frame document of the example framed page:


<html>
<head>
  <title>Frames Example</title>
</head>
<frameset rows="50%,*" border=1>
  <frameset cols="50%,*">
    <frame src="tile1.html">
    <frame src="tile2.html">
  </frameset>
  <frameset cols="60%,*">
    <frame scrolling="yes" src="tile3.html">
    <frame src="tile4.html" name="four">
  </frameset>
</frameset>
</html>
<frameset rows="50%,*" border="1">
This tag begins by setting the size of the two rows, in this case. By specifying '50%' the first row will be half the size of the screen. The '*' means 'the rest of'; since in this case the first row takes up half the screen, the remainder of the screen is used by the second row. It is possible to specify a pixel value for the size, but since screen size may vary it is often better to give relative values.
<frameset cols="50%,*">
This tag, since it is nested within the other frameset, will split the top row into, in this case, two pieces. This tag is setting the column size to half the screen, therefore both frames will be the same size.
<frame src="tile1.html">
<frame src="tile2.html">
These two frame tags are loading the html pages that will appear in the top two columns. The first instance of the frame tag loads the first HTML file, while the remaining instances load pages into the frames in the order that they are listed.
</frameset>
This ending tag closes the first row. Therefore, at this point, there are two pages loaded in the first row and the second row is blank.
<frameset cols="60%,*">
This frameset tag will now control the second (bottom) row. This time it sets the first column of the second row to 60%; thus the second column will be 40% the size of the screen.
<frame scrolling="yes" src="tile3.html">
This frame tag loads the HTML document 'tile3' into the first column of the second row. The scrolling attribute controls the frame's ability to be scrollable (as well as whether it has a scrollbar). Since the attribute is set to 'yes' this frame is scrollable.
<frame src="tile4.html" name="four">
This tag loads the final HTML document, placing it into the second row, second column. The name attribute gives the frame a name. This is used to load documents into a frame using a link contained within a separate frame. This is accomplished through the use of the target attribute of the <a href ... > tag. For example:
<a href="doc1.html" target="frame2">
This tag will load the HTML document 'doc1' into the frame named 'frame2'. This is how the example was able to change frame #4 to frame #5. Also, in order to load a document full screen instead of inside a frame, use the target value "_top".
<a href="fullscreen.html" target="_top">

Good Web Publishing Techniques

In order to create effective web pages, it is not enough to know how to program in HTML. It is also important to understand what is included in well designed web pages. The design and lay out of a web page is as important as the information it contains. The following is a list of things to consider when designing an effective web page: