Table Attributes

Attributes to Use with the <table> Tag
AttributePurpose and SyntaxValues
bgcolorDefines the background color of the table
<table bgcolor="blue">
16 preset colors or hexadecimal colors
borderDefines the line weight of the border
<table border="1">
If border is not specified, the table will not have a border.
a number
bordercolorDefines the color of the border
<table bordercolor="red">
16 preset colors or hexadecimal colors
cellpaddingDefines the vertical spacing within the cells
<table cellpadding="2">
the number of pixels
cellspacingDefines the spacing between cells
<table cellspacing="1">
the number of pixels
heightDefines the height of the table
<table height="200">
the number of pixels or a percentage of the full window size, e.g., "30%"
widthDefines the width of the table
<table width="200">
the number of pixels or a percentage of the full window size, e.g., "30%"

Attributes to Use with the <tr> Tag
AttributePurpose and SyntaxValues
alignDefines the horizontal alignment of the text within the table row
<tr align="center">
left, right, center
bgcolorDefines the background color of the table row
<tr bgcolor="blue">
16 preset colors or hexadecimal colors

Attributes to Use with the <td> and <th> Tags
AttributePurpose and SyntaxValues
alignDefines the horizontal alignment of the text within the table cell
<td align="center">
The <td> tag alignment takes precedence over the <tr> tag alignment.
left, right, center
bgcolorDefines the background color of the table cell
<td bgcolor="blue">
16 preset colors or hexadecimal colors
colspanInstructs the browser to stretch the cell across a specified number of columns
<td colspan="3">
the number of columns
heightDefines the height of the cell
<td height="200">
the number of pixels or a percentage of the table size, e.g., "30%"
nowrapInstructs the browser not to wrap the text within the table cell
<td nowrap>
 
rowspanInstructs the browser to stretch the cell over a specified number of rows
<td rowspan="2">
the number of rows
valignDefines the vertical alignment of the text within the table cell
<td valign="top">
top, middle, bottom
widthDefines the width of the table cell
<td width="200"> or <td width="50%">
the number of pixels or a percentage of the table

Attributes to Use with the <caption> Tag
AttributePurpose and SyntaxValues
alignDefines the position of the caption text
<caption align="bottom">Caption Text</caption>
top, bottom

Example of Table Attributes:
<table width="200" border="1">
     <caption align="bottom">Table Caption</caption>
     <tr align="center">
          <th colspan="2">Table Heading</th>
     </tr>
     <tr align="left">
          <td width="50%">First Item</td>
          <td width="50%">Second Item</td>
     </tr>
</table>
 
Table Caption
Table Heading
First Item Second Item
Note: When you set both a <tr> tag and a <td> tag for the same attribute, the <td> attribute takes precedence. For example, if you set bgcolor in both tags, the bgcolor attribute in the <td> tag will be used.

 

Back to the Week 5 Schedule