Learn HTML Table Tag : Elements and Examples

Mastering HTML Tables: Elements and Examples

HTML tables are powerful tools for organizing and displaying data in a structured format. In this article, we’ll explore the various elements of HTML tables and provide a comprehensive example.

Basic Table Structure:

The <table> element is the container for all other table elements. A table consists of rows ( <tr> ) and cells ( <td> for data cells , and <th> for header cells). The header cells typically contain labels for the columns or rows.

HTML Table Tag Example:

<!DOCTYPE html>
<html>
<head>
<title>HTML Table Example</title>
</head>
<body>
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Data 1.1</td>
<td>Data 1.2</td>
<td>Data 1.3</td>
</tr>
<tr>
<td>Data 2.1</td>
<td>Data 2.2</td>
<td>Data 2.3</td>
</tr>
</table>
</body>
</html>

Output Of Above Code :

Table Tag Elements:

1.<table> :
The container for the entire table.

2.<tr> :
Defines a table row. Contains one or more cells.

3.<th> :

Defines a header cell. Typically used in the first row or first column to label the data.

4. <td> :

Defines a data cell. Contains the actual data of the table.

Attributes:

border : Specifies the border width of the table. It is commonly used for styling purposes, although CSS is recommended for more control.

Table Example:

Let’s break down the example above:

<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Data 1.1</td>
<td>Data 1.2</td>
<td>Data 1.3</td>
</tr>
<tr>
<td>Data 2.1</td>
<td>Data 2.2</td>
<td>Data 2.3</td>
</tr>
</table>

– The <table> element is the container for the entire table.
– Each <tr> represents a row. The first row contains <th> for headers, and subsequent rows contain <td> for data.
– The border=”1″ attribute adds a border to the table for visual clarity (Note: styling with CSS is recommended).

Additional Attributes and Styling:

1.colspan and rowspan :

These attributes allow a cell to span multiple columns or rows.

<td colspan="2">Spanning two columns</td>

2. CSS Styling:

Use CSS for more advanced styling and layout control.

For example:

table {
width: 100%;
border-collapse: collapse;
}

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

This CSS ensures a clean, border-collapse layout with proper padding for cells.

Understanding HTML tables and their elements empowers you to present data effectively on your web pages. In upcoming articles, we’ll explore advanced table styling with CSS and dynamic table manipulation using JavaScript.

Leave a Reply

Your email address will not be published. Required fields are marked *