HTML: Tables

Tables are used for setting up information in a tabular form. They allow for information to be displayed in rows and columns. Tables can contain text as well as HTML elements such as images and select lists. Here are some examples:

Monday Tuesday
Notre Dame Princess
7:00 - 9:00 7:00 - 9:00

Picture Your Answer
This is a:

The tags used for building tables are:

<table> .. </table> - starts and stops the TABLE - contains the table row and table data/head tags which follow below

<tr> .. </tr> - identifies the start and end of a TABLE ROW - contains the table data/head tags

<td> .. </td> - identifies the start and end of a TABLE CELL - contains the information (text and other html elements) in the cell

<th> .. </th> - identifies the start and end of the TABLE CELL - same as <td> except it bolds and centers any text within.


Here is an example of a basic table and the HTML to construct it:

First Heading Second Heading
Data in first cell Data in second cell

<style>
#firstexample {width: 90%; border: 1px solid #000000;}
#firstexample th {border: 1px solid #000000;}
#firstexample td {border: 1px solid #000000;}
</style>

<table id="firstexample">
<tr>
<th>First Heading</th>
<th>Second Heading</th>
</tr>
<tr>
<td>Data in first cell</td>
<td>Data in second cell</td>
</tr>
</table>


Cellpadding and Cellspacing

description example what it does
cellpadding cellpadding="5" an equal amount of space within the cell in pixels
cellspacing cellspacing="10" the amount of space between the cells themselves in pixels

Here is an example an earlier table with cellpadding and cellspacing both set to 10.

First Heading Second Heading
Data in first cell Data in second cell

Note that if a value for cellpadding or cellspacing is not entered a default value of 2 will be applied.
Colspan and Rowspan
The last thing we need to discuss with tables is COLSPAN and ROWSPAN, which are additonal attributes for the table data tag.

COLSPAN
Suppose you had 2 columns, but you only wanted 1 header for the columns, you could use the COLSPAN attribute within the TABLE DATA tag.
Restaurants
McDonald's Bangkok Thai
A&W Boston Pizza

In the example above the HTML looks like this:

<style>
#colspanexample {width: 90%; border: 1px solid #000000;}
#colspanexample th {border: 1px solid #000000;}
#colspanexample td {border: 1px solid #000000;}
</style>

<table id="colspanexample" >
<tr>
<th colspan="2">Restaurants</th>
</tr>
<tr>
<td>McDonald's</td>
<td>Bangkok Thai</td>
</tr>
<tr>
<td>A&amp;W</td>
<td>Boston Pizza</td>
</tr>
</table>


ROWSPAN
Rowspan works the same way as colspan, except that the cells span multiple rows.

Restaurants McDonald's Bangkok Thai
A&W Boston Pizza

In the example above the HTML looks like this:

<style>
#rowspanexample {width: 90%; border: 1px solid #000000;}
#rowspanexample th {border: 1px solid #000000;}
#rowspanexample td {border: 1px solid #000000;}
</style>


<table id="rowspanexample">
<tr>
<th rowspan="2">Restaurants</th>
<td>McDonald's</td>
<td>Bangkok Thai</td>
</tr>
<tr>
<td>A&amp;W</td>
<td>Boston Pizza</td>
</tr>
</table>


Table Width and Height
By default tables and cells will expand as needed to contain whatever it is you put within them. You can set the width and height and the browser will honor the setting as long as you don't place an HTML element within it that is wider or taller than what you have provided for. Although you will often want to control the width of a table or other block level HTML element to ensure the user never has to horizontally scroll to see the full web page, seldom will you need to control the height as you will just let the cell or table expand to accomodate whatever is within it. You can set the width and height as:

width: value (value can = absolute amount of pixels or a relative %)
height: value (value can = absolute amount of pixels or a relative % although relative heights are not supported across all browsers.)

First Heading Second Heading
Data in first cell Data in second cell

Here is the HTML source for the example above (styles indicated in the bold typeface):

<style>
#widthexample {border: 1px solid #000000;}
#widthexample th {border: 1px solid #000000;}
#widthexample td {border: 1px solid #000000;}
</style>


<table id="widthexample" style="width:300px; height: 140px;">
<tr>
<th>First Heading</th>
<th>Second Heading</th>
</tr>
<tr>
<td>Data in first cell</td>
<td>Data in second cell</td>
</tr>
</table>


Horizontally and Vertically Aligning Cell Content
By default content placed within a cell with be horizontally aligned to the left and vertically aligned to the middle. You can change this with the following styles:

text-align: value (value can = left, right, center, justify)
vertical-align: value (value can = top, middle, bottom)

First Heading Second Heading
Data in first cell Data in second cell

Here is the HTML source for the example above (styles indicated in the bold typeface):

<style>
#alignexample {width: 90%; border: 1px solid #000000; height: 100px;}
#alignexample th {border: 1px solid #000000;}
#alignexample td {border: 1px solid #000000;}
</style>


<table id="alignexample">
<tr>
<th>First Heading</th>
<th>Second Heading</th>
</tr>
<tr>
<td style="text-align:center; vertical-align:top;">Data in first cell</td>
<td style="text-align:right; vertical-align:bottom;">Data in second cell</td>
</tr>
</table>


Borders and Tables
Often you will want to have borders appear around tables and either around the table rows and the table data cells. Visual formatting of tables is best done through an internal or external set of style rules. Note that the border properties shown below can be applied to most HTML elements and are not specific to tables.

Consider the following options when defining borders for your tables:
Property Description Values
border
For setting all of the properties of the four borders in one declaration

example:

table {border: 2px solid #0000ff;}
border-width
border-style
border-color
border-top
border-bottom
border-left
border-right
For setting all of the properties of one border in one declaration

example:

table {border-bottom: 2px solid #0000ff;}
border-bottom-width
border-style
border-color
border-width For setting all of the properties of the four borders in one declaration, can have from one to four values thin
medium
thick
width in pixels
border-style Sets the style of the four borders, can have from one to four styles none
hidden
dotted
dashed
solid
double
groove
ridge
inset
outset
border-color Sets the color of the four borders, can have from one to four colors color - name or hex value

Backgrounds and Tables
Often you will want to have backgrounds (colors ususally) appear behind tables and behind table rows and the table data cells. Visual formatting of tables is best done through an internal or external set of style rules. Note that the background properties shown below can be applied to most HTML elements and are not specific to tables.

Consider the following options when defining backgrounds for your tables:

Property Description Values
background A shorthand property for setting all background properties in one declaration background-color
background-image
background-repeat background-attachment background-position
background-color Sets the background color of an element color-rgb
color-hex
color-name
transparent
background-image Sets an image as the background url(URL)
none
background-attachment Sets whether a background image is fixed or scrolls with the rest of the page scroll
fixed
background-position Sets the starting position of a background image top left
top center
top right
center left
center center
center right
bottom left
bottom center
bottom right
x% y%
xpos ypos
background-repeat Sets if/how a background image will be repeated repeat
repeat-x
repeat-y
no-repeat

 


modifed: 2009-04-13
Google
web www.digitalvertebrae.com
Exercises
The exercises for this week are listed below and available in PDF or MS Word formats.
Determining the Sites Structure
HTML - Tables
CSS - Practice CSS Layouts
 
©2009 Web Site Development: an introduction