HTML Tables
HTML Table Tags and Properties Explained by CodesWithPankaj
HTML tables are created using a combination of tags to structure data in rows and columns. Let's dive into each tag used in HTML tables, along with the properties they offer for creating well-structured and organized tables.
1. <table>
Tag – Table Container
<table>
Tag – Table ContainerThe <table>
tag is the main container that holds the entire table structure, including rows, columns, headers, and data cells.
Example:
border
: Adds a border around the table and its cells (usually1
or0
).width
andheight
: Define the table's width and height.align
: Aligns the table (e.g.,left
,right
,center
). This attribute is deprecated and replaced with CSS.
2. <tr>
Tag – Table Row
<tr>
Tag – Table RowThe <tr>
tag defines a single row in a table. Rows can contain header cells (<th>
) or regular cells (<td>
).
Example:
3. <th>
Tag – Table Header Cell
<th>
Tag – Table Header CellThe <th>
tag defines a header cell, which is usually displayed in bold and centered by default. It helps categorize the data in the columns.
Example:
scope
: Defines the cell’s scope, indicating whether the header applies to a row (row
), column (col
), or a group of rows/columns.
4. <td>
Tag – Table Data Cell
<td>
Tag – Table Data CellThe <td>
tag is used to create a data cell within a row. Data cells hold the content of the table.
Example:
colspan
: Defines the number of columns a cell should span across.rowspan
: Defines the number of rows a cell should span across.
Example of colspan
and rowspan
:
5. <caption>
Tag – Table Caption
<caption>
Tag – Table CaptionThe <caption>
tag is used to provide a title or description for the table. It is displayed above the table by default.
Example:
6. <colgroup>
Tag – Column Grouping
<colgroup>
Tag – Column GroupingThe <colgroup>
tag is used to group one or more columns together for styling purposes. This is useful for applying styles to specific columns.
Example:
7. <col>
Tag – Column Definition
<col>
Tag – Column DefinitionThe <col>
tag is used within <colgroup>
to specify properties for each column.
span
: Specifies how many columns a<col>
element should span.
8. <tbody>
Tag – Table Body
<tbody>
Tag – Table BodyThe <tbody>
tag groups the main content (body) of the table, separating it from the header (<thead>
) and footer (<tfoot
).
Example:
9. <thead>
Tag – Table Header Group
<thead>
Tag – Table Header GroupThe <thead>
tag is used to group header rows, allowing browsers to render them differently and repeat them if the table spans multiple pages.
10. <tfoot>
Tag – Table Footer Group
<tfoot>
Tag – Table Footer GroupThe <tfoot>
tag is used to group footer rows, typically containing summary data or totals. This appears at the bottom of the table.
Example:
Full Example of HTML Table with All Tags
Here's a complete example incorporating all the above tags and properties:
Summary
Each HTML table tag has a specific role, helping organize, style, and group data in a structured way. Using these tags correctly improves the readability and functionality of your tables. Experiment with each tag to get a strong foundation in creating efficient tables with CodesWithPankaj!
Last updated