In HTML, tables are created using the <table>
element and are composed of rows and cells. Here’s an example of the basic structure of an HTML table:
<table> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> </tr> <tr> <td>Row 2, Cell 1</td> <td>Row 2, Cell 2</td> </tr> </table>
Let’s break down the structure and properties of the table:
<table>
: This is the container element for the entire table.<tr>
: Stands for “table row” and represents a row within the table.<th>
: Represents a header cell in the table. It is typically used within the<tr>
element to define column headers.<td>
: Represents a data cell in the table. It is used within the<tr>
element to define the data for each cell.<caption>
: An optional element that can be used to add a caption to the table. It should be placed immediately after the opening<table>
tag.
Apart from the basic structure, there are several properties that can be applied to tables using HTML attributes:
colspan
: Specifies the number of columns a cell should span.rowspan
: Specifies the number of rows a cell should span.align
: Aligns the content within a cell horizontally. Values can be “left,” “right,” or “center.”valign
: Aligns the content within a cell vertically. Values can be “top,” “middle,” or “bottom.”border
: Specifies the width of the border around the table and its cells.bgcolor
: Sets the background color of a table or a specific cell.width
: Sets the width of the table or a specific cell. It can be specified in pixels or as a percentage.height
: Sets the height of the table or a specific cell. It can be specified in pixels or as a percentage.
These are just some of the commonly used properties and attributes for tables in HTML. There are additional properties and styling options available through CSS to further customize the appearance and behavior of tables.