MCCC CIS 177 - Markup Languages

4.0 Tables

4.2 Customizing tables

Borders

As previously mentioned you can optionally add borders around your table simply by using the border="n" attribute where "n" is the number of pixels wide. IN IE ONLY, you can also choose which borders you want to display by using the frame="location" attribute where location is as follows:

  • vsides for vertical sides
  • hsides for horizontal sides.
  • above for a single top border
  • below for a single bottom border
  • rhs for a single border on the right
  • lhs for a single border on the left

Place the frame attribute after the border attribute - without the border value - which is required for this feature to work. The following code illustrates the frame="hsides" which displays only the external horizontal borders, however does not display the external vertical borders. Notice the attribute is also used with align, valign, width and height attributes.

<table border frame="hsides" align="center" valign="middle" width="150" height="150">
    <tr>
    <td align="center" valign="top">R1C1</td>
    <td align="center" valign="top">R1C2</td>
    <td align="center" valign="top">R1C3</td>
    </tr>

    <tr>
    <td align="center" valign="top">R2C1</td>
    <td align="center" valign="top">R2C2</td>
    <td align="center" valign="top">R2C3</td>
    </tr>

</table>
R1C1 R1C2 R1C3
R2C1 R2C2 R2C3

Experiment with Table Borders

Create a new HTML document or use the one previously created.

Create a table containing two columns and four rows containing the data as shown.

Set your borders to appear as in the example.

TIP: The table includes cellspacing and cellpadding

Save the file and view the file in the browser

Name Phone
Mary 555-1234
John 444-9876
Sue 999-4567

Special tags

While the <table>, <tr> and <td> tags are the most commonly used table tags, you may optionally use the following tags to customize your table:

  • <th> - table header: use in place of <td> to automatically center and bold the cell content
  • <caption> - caption tag: use immediately following the opening <table> tag to create a caption.

<table border="1" align="right" width="50%">
<caption align="top">
Example of Caption and TH tags</caption>

    <tr>
    <th width="50%">1st Heading</th>
    <th width="50%">2nd Heading </th>

    </tr>
    <tr>
    <td>
    1st data</td>
    <td>
    2nd data</td>

</table>

Example of Caption and TH tags
1st Heading 2nd Heading
1st data 2nd data

Specify Caption and Table Heading Tags

Edit the table you previously made so that it looks like the one below using the caption and th tags appropriately.

NOTE: Set your table width to 80% and align the table in the center of the page.


Save the file and view it in the browser

Edit the code to make the Tahiti and Virgin Island rows 200 pixels high and vertically align the data to the top of the cell.

Save the file and refresh the browser.

Edit the code to horizontally center all data.

Save the file and refresh the browser.

Tour Packages
Destination Description
Tahiti You will enjoy five fun-filled nights among the Tahitian islands. The first night will be spent in Tahit. Nights two and three will be spent on Bora Bora. On night four, you will enjoy a sumptuous feast on Moorea. Your final night will be spent on the water off the island of Tahiti.
Virgin Islands For this tour, the cruise ship will be your hotel. Stops are made at several of the most beautiful islands. Nights you will enjoy the sound of Carribean festival music as you dine oat our 24 hour buffet.

 

4.29 Backgrounds

Background Colors

You can optionally add color to your table as well as to the individual rows and data cells as specified in the example below.

<table border="1" align="right" width="50%" bgcolor="cyan" cellspacing="15">
<caption align="top">
Example of Table Background Colors

    <tr bgcolor="magenta">
    <th width="50%">1st Heading</th>
    <th width="50%">2nd Heading </th>

    </tr>
    <tr>
    <td bgcolor="white">
    1st data</td>
    <td bgcolor="yellow">
    2nd data</td>

</table>

Example of Table Background Colors
1st Heading 2nd Heading
1st data 2nd data

Background Tiles

Alternatively you can tile a background image in a table in a similar way you can in the body tag. Keep in mind you can use both bgcolor and background together, however, in NS, the bgcolor disappears when you specify a background image while in IE if you specify an image in a <td;>, tag, IE will use bgcolor in any cellspacing you've specified. Also, when you specify a background image for the whole table in the <table> tag, IE uses one image for the background, while NS tiles the image in each table cell.

<table width="350" height="300" align="right" cellspacing="15" cellpadding="0" border="1" bgcolor="black">

    <tr> <td width="50%" height="135" background="yamaha.jpg" align="center" >Ready to ride?
    </td> <td width="50%" height="135" background="bg03.gif" align="center">Ready to have some fun?
    </td>
    </tr>
    <tr>
    <td width="50%" height="135" bgcolor="blue" align="center">Ready to have fun?
    </td>
    <td width="50%" height="135" bgcolor="green" align="center">Ready to ride?
    </td>
    </tr>

</table>

 

Ready to ride? Ready to have some fun?
Ready to have fun? Ready to ride?