Chapter
12
In this exercise you will learn the tags used to create a table. You will also crate a table and insert information in the table cells.
A table consists of rows, which run horizontally, and columns, which run vertically. Rows and columns intersect to create cells.
Three tags are used to create an HTML table. The <TABLE> tag tells the browser where to begin the table. The <TABLE> tag also includes the specifications for the table, such as table size and border width. The <TR> tag defines a row of cells. The <TD> tag indicates table data and is used to specify a table cell. You do not need to specify the number of columns in your table, just the number of cells in each row.
To create a table, do the following steps:
First, you have to specify where the table should begin and end.
Next, you need to define a table row.
Now you need to define the table cell and enter the cell data.
Note: You must use the <TD> and </TD> tags for each cell in the row. Also, you must use the <TR> and </TR> tags for each row in the table.
You should see only two names on the page. By default, tables do not have borders, or lines around the table and cells. Later, you will add the border for your table.
Add another row to the table by doing the following steps:
Notice you have to define each row in the table, and within each row, you must define each cell. To make your table tags easier to read, you can insert blank lines in your text editor as shown in the figure below:
Table borders are specified using the BORDER=# attribute. The number is the width of the border. The attribute is placed inside the <TABLE> tag, like this: <TABLE BORDER=5>.
To create a border for the table, do the following step:
To change the width of the border, do the following steps:
Notice the border is getting thicker as the number get larger.
Your table should look like the figure below. If your table does not look like the example, double-check your source code and make sure you keyed the tags correctly.
In this exercise you will change the size of the table. You will also format the table cells.
Changing the Table Size
You can change the width (from left to right) and height (from top to bottom) of your table similarly to the way you changed the size of the border.
When no width is specified, the table is only as long as it needs to be. Notice each cell in Figure 12-3 is only as long as the longest text in each column.
When changing the width of the table, you need to decide what percentage of the window’s width you want the table to occupy. For example, a table width of 50% would take up 50% (half) of the width of your document window. A table width of 100% would go across the whole window.
To change the width of your table, do the following steps:
The tag should now read <TABLE BORDER=3 WIDTH=50%>.
The table should be halfway across your Web page.
The table should be as wide as your Web page.
Changing the height is similar to changing the border size. The height is changed using numbers instead of percentages. The default border size is none, or zero. Each number increases the size of the border. For example, BORDER=3 would produce a thin border, and BORDER=75 would produce a very thick border.
To change the height of the table, do the following steps:
The tag should now read <TABLE BORDER=3 WIDTH=50% HEIGHT=100>.
You can specify how the data in the table cell will appear by aligning the text horizontally, vertically, or both.
To align the text horizontally (left to right), use the ALIGN=XXX inside the <TD> tag. XXX can be either left, center, or right. The default is left.
To align the text vertically (top to bottom), use the VALIGN=XXX inside the <TD> tag. XXX can be either top, middle, or bottom. The default is middle.
To change the horizontal alignment of a cell, do the following steps:
Notice your name is the only one centered. Each cell must be individually formatted.
To change the vertical alignment of a cell, do the following steps:
Now let’s change the format for all the cells in the first row.
Now let’s change the format for all the cells in the second row.
Your table should look like the Figure below. If your table does not look like the figure, double-check your source code and make sure you keyed the tags correctly.
|
Define the table |
<TABLE> </TABLE> |
|
Define table with border |
<TABLE BORDER> </TABLE> |
|
Define table with specific border size |
<TABLE BORDER=#> </TABLE> |
|
Specify desired table width |
<TABLE WIDTH=#> </TABLE> |
|
Define table row |
<TR> </TR> |
|
Define table cell |
<TD> </TD> |
|
Horizontally aligns data in the cell |
<TD ALIGN=LEFT/CENTER/RIGHT> |
|
Vertically aligns data in the cell |
<TD VALIGN=TOP/MIDDLE/BOTTOM |
|
Specify number of columns for cell to span |
<TD COLSPAN=#> |
|
Specify number of rows for cell to span |
<TD ROWSPAN=#> |
In this exercise you will use what you have learned to create another table.
Exercise 4
In this exercise you will use what you have learned to create a Web page table.