designed by Lara Labastie
A table can be a powerful tag to use in the format of a Web document. It allows you to
create perfectly straight columns such as in a magazine layout, or meaningful and
appealing matrices of data.
Tables are created using sets of paired tags, just like the other tags you are familiar with in HTML. The following is a list of the essential table tags.
Tag | Description |
---|---|
<TABLE>...</TABLE> | Defines a table |
<TR>...</TR> | Defines a table row |
<TD>...</TD> | Defines a cell within a row |
What you do: | What you see: | ||||
<TABLE> <TR><TD>Cell 1</TD><TD>Cell 2</TD></TR> <TR><TD>Cell 3</TD><TD>Cell 4</TD></TR> </TABLE> |
|
||||
What you try:
|
You can also add a caption and row and column headings to your table by using the <CAPTION> and </CAPTION> and the <TH> and </TH> tags.
Tag | Description |
---|---|
<CAPTION>...</CAPTION> | Creates a centered table title |
<TH>...</TH> | Creates centered, bold column or row headings |
What you do: | What you see: | ||||||||||||
<TABLE> <CAPTION>Hours of Business</CAPTION> <TR><TH></TH><TH>Hours</TH></TR> <TR><TH>Mon</TH><TD>8 am to 5 pm</TD></TR> <TR><TH>Tue</TH><TD>8 am to 5 pm</TD></TR> <TR><TH>Wed</TH><TD>8 am to 5 pm</TD></TR> <TR><TH>Thr</TH><TD>8 am to 5 pm</TD></TR> <TR><TH>Fri</TH><TD>8 am to 4 pm</TD></TR> </TABLE> |
|
||||||||||||
What you try:
|
Now that you've been introduced to the essentials of a table, here are a few attributes you can use to make your tables more interesting. To use any of these attributes, you must include them inside your first <TABLE> tag.
Option | Description |
---|---|
BORDER=(any number from 0 to 10) | Creates a border |
CELLPADDING=(any number in pixels) | Adds white space around the contents of each cell |
CELLSPACING=(any number in pixels) | Adds white space between cells |
WIDTH=(1% - 100%) | Defines the table width in proportion to the page |
What you do: | What you see: | |||||||||
<TABLE BORDER=5 WIDTH=100%> <TR><TD>1,1</TD><TD>1,2</TD><TD>1,3</TD></TR> <TR><TD>2,1</TD><TD>2,2</TD><TD>2,3</TD></TR> <TR><TD>3,1</TD><TD>3,2</TD><TD>3,3</TD></TR> </TABLE> |
|
|||||||||
What you try:
|
What you do: | What you see: | |||||||||
<TABLE BORDER=1 CELLPADDING=20 WIDTH=100%> <TR><TD>1,1</TD><TD>1,2</TD><TD>1,3</TD></TR> <TR><TD>2,1</TD><TD>2,2</TD><TD>2,3</TD></TR> <TR><TD>3,1</TD><TD>3,2</TD><TD>3,3</TD></TR> </TABLE> |
|
|||||||||
What you try:
|
What you do: | What you see: | |||||||||
<TABLE BORDER=1 CELLSPACING=10 WIDTH=100%> <TR><TD>1,1</TD><TD>1,2</TD><TD>1,3</TD></TR> <TR><TD>2,1</TD><TD>2,2</TD><TD>2,3</TD></TR> <TR><TD>3,1</TD><TD>3,2</TD><TD>3,3</TD></TR> </TABLE> |
|
|||||||||
What you try:
|
The attributes you just learned are applied to the entire table. Now lets learn a few attributes that can control an individual cell or an entire row of cells. To use any of these next tags you must include them inside your <TR>, <TH>, or <TD> tag.
Option | Description |
---|---|
ALIGN=(left, center, or right) | Aligns the cell's text accordingly |
ROWSPAN=(any number of valid rows) | Allows a cell to span multiple rows |
COLSPAN=(any number of valid columns) | Allows a cell to span multiple columns |
What you do: | What you see: | ||||||||
<TABLE BORDER=5 CELLPADDING=10> <TR><TH></TH><TH>A Long Heading</TH> <TR ALIGN=LEFT><TH>Left</TH><TD>No</TD></TR> <TR ALIGN=CENTER><TH>Center</TH><TD>Maybe</TD></TR> <TR ALIGN=RIGHT><TH>Right</TH><TD>Yes</TD></TR> </TABLE> |
|
||||||||
What you try:
|
What you do: | What you see: | ||||||||||||
<TABLE BORDER=1 CELLPADDING=10> <CAPTION>Column Spanning</CAPTION> <TR><TH>Col 1</TH><TH>Col 2</TH><TH>Col 3</TH></TR> <TR><TD COLSPAN=2>Col 1 & 2</TD><TD>Col 3</TD></TR> <TR><TD>Col 1</TD><TD COLSPAN=2>Col 2 & 3</TD></TR> <TR><TD COLSPAN=3>Col 1, 2, & 3</TD></TR> </TABLE> |
|
||||||||||||
What you try:
|
What you do: | What you see: | |||||||
<TABLE BORDER=1 CELLPADDING=10> <CAPTION>Row Spanning</CAPTION> <TR><TH>Row 1</TH><TD ROWSPAN=2>Row 1 & 2</TD><TD>Row 1</TD></TR> <TR><TH>Row 2</TH><TD ROWSPAN=2>Row 2 & 3</TD></TR> <TR><TH>Row 3</TH><TD>Row 3</TD></TR> </TABLE> |
|
|||||||
What you try:
|
Great job! You now know the basics for creating great tables. There are a few more advanced techniques that you can learn about on your own, but not many more. Remember, you can put anything into a table cell (links, images, text, etc.) Have fun, and good luck creating your Web pages!
Lara Labastie