Skip to main content

Creating Tables in HTML

Creating tables in HTML is a common task when building web pages. Tables are used to display data in rows and columns, making it easier to organize and present information. In this tutorial, you will learn how to create tables in HTML using the <table>, <tr>, and <td> tags.

This guide will explore how to construct tables step by step and demonstrate using different elements and attributes.

Step 1: Start with the <table> Element​

To create a table, you start with the <table> tag, which serves as the container for all table content. Inside this tag, you define rows and cells.

Example:

table-example.html
<table>
<!-- Table content goes here -->
</table>

Step 2: Add Table Rows (<tr>) and Cells (<td>)​

Inside the <table> element, you define rows using the <tr> tag. Each row contains one or more cells, which are created using the <td> tag. Cells are the individual data points within the table.

Example:

table-example.html
<table>
<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>

In the example above, we have a table with two rows, each containing two cells. The table structure is as follows:

http://.../index.html
Row 1, Cell 1Row 1, Cell 2
Row 2, Cell 1Row 2, Cell 2

Step 3: Add Table Headings (<th>)​

In addition to regular cells, you can use the <th> tag to define table headings. Headings are displayed in bold by default and are useful for labeling rows or columns.

Example:

table-example.html
<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>

In the example above, we have added table headings to the first row of the table. The headings are displayed in bold:

http://.../index.html
Header 1Header 2
Row 1, Cell 1Row 1, Cell 2
Row 2, Cell 1Row 2, Cell 2

Step 4: Add Table Captions (<caption>)​

You can include a caption for the table using the <caption> tag. The caption appears above or below the table and provides a brief description or title for the table content.

Example:

table-example.html
<table>
<caption>Sample Table</caption>
<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>

In the example above, we have added a caption to the table:

http://.../index.html
Sample Table
Header 1Header 2
Row 1, Cell 1Row 1, Cell 2
Row 2, Cell 1Row 2, Cell 2

Step 5: Styling the Table​

You can style the table using CSS to change its appearance, such as the background color, text color, border, padding, and spacing. Here is an example of styling the table using CSS:

styles.css
table {
width: 100%;
border-collapse: collapse;
}

th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}

th {
background-color: #f2f2f2;
}

In this CSS code snippet, we set the table width to 100%, collapse the borders, add padding to cells, align text to the left, and style the table headings with a light gray background.

Conclusion​

In this tutorial, you learned how to create tables in HTML using the <table>, <tr>, <td>, <th>, and <caption> tags. Tables are essential for displaying data in a structured format on web pages. You can further enhance the appearance of tables by applying CSS styles to customize their look and feel.