HTML Tables



<table> — used to create a table

• Tables are divided into rows using the <tr> tag
• Each row contains table data (columns) using the <td> tag

Syntax:

<table>
  <tr>
    <td>Table Data</td>
  </tr>
</table>

Example:

[Row 1, Column 1] [Row 1, Column 2] [Row 1, Column 3]
[Row 2, Column 1] [Row 2, Column 2] [Row 2, Column 3]


Table Attributes

border - sets the border width of the table (in px)

bordercolor - sets the border color

cellpadding - adds space between cell content and cell walls

cellspacing - sets the spacing between table cells

height - sets the height of the table

width - sets the width of the table

Syntax example:

<table border="2" cellpadding="5" cellspacing="5" width="400px"></table>


Table Data Attributes

align - horizontal alignment: left, center, right

background - sets an image as table background

bgcolor - sets background color

bordercolor - sets border color

Syntax:

<td align="center" bgcolor="yellow" bordercolor="black">Sample</td>


colspan Attribute

• Used to make one cell span across multiple columns

Example:

Red Blue Green
Yellow Violet



rowspan Attribute

• Allows one cell to span across multiple rows

Example:

Red Blue Green
Yellow Orange
Violet Pink



Table Header

• <th> defines header cells
• Header text is bold and centered by default

Example:

Sample 1 Sample 2
Red Blue Green
Yellow Orange



Caption

• Adds a title to a table
• Usually placed directly after the <table> tag

Example:

Sample Table
Row 1, Column 1 Row 1, Column 2 Row 1, Column 3
HTML Logo