In this chapter, we will explore HTML tables from the basics to advanced topics. Tables in HTML are essential for organizing and displaying data in a structured, grid-like format. They are composed of rows and columns, making it easy to represent information such as schedules, datasets, and more. We will cover how to create, style, and manipulate tables using HTML elements and attributes.
HTML tables are used to display tabular data in a web page. They provide a clean and efficient way to present data that needs to be organized in rows and columns, such as financial reports, timetables, or lists.
The basic HTML elements used to create tables are:
<table>
: Defines the overall table.<tr>
: Defines a table row.<th>
: Defines a table header (for headings).<td>
: Defines a table data cell (for regular data).
Name
Age
Alice
25
Bob
30
<table>
element wraps the entire table.<tr>
(table row) contains table headers (<th>
) or data cells (<td>
).Name | Age |
---|---|
Alice | 25 |
Bob | 30 |
Tables in HTML are defined row by row. The simplest structure consists of a table element containing rows, with each row containing a set of cells (either headers or data).
<table>
).<tr>
tag.<th>
, and data is displayed using <td>
.
Product
Price
Apples
$1.50
Bananas
$1.20
Product | Price |
---|---|
Apples | $1.50 |
Bananas | $1.20 |
The <th>
element is used to create table headers. Table headers are bold and centered by default. They provide context for the data below them.
City
Population
New York
8,398,748
Los Angeles
3,990,456
City | Population |
---|---|
New York | 8,398,748 |
Los Angeles | 3,990,456 |
Headers are particularly useful when tables have a lot of data, ensuring the viewer knows what each column represents.
You can make a cell span across multiple columns or rows using the colspan
and rowspan
attributes. This is useful for merging cells.
Product Information
Product
Price
Orange
$0.75
Product Information |
---|
Product |
Orange |
Here, the header spans two columns.
Day
Event
Monday
Meeting
Workshop
Day | Event |
---|---|
Monday | Meeting |
Workshop |
The “Monday” cell spans two rows.
Tables can be styled using CSS to improve readability and design. Common styles include borders, background colors, padding, and text alignment.
Item
Quantity
Bread
2
Milk
1
The border-collapse: collapse
ensures that adjacent cell borders merge, and the background color for the header is light gray.
You can create tables within tables to display complex hierarchical data. This is called nesting.
Department
Employees
HR
John
Mary
Department | Employees |
---|---|
HR | John |
Mary |
The nested table is placed inside the HR department row, showing employees in that department.
To make your tables accessible, follow these best practices:
<caption>
element to add a title to the table."scope"
attribute in table headers to define their role (row or column header).
Monthly Sales Report
Month
Sales
January
$10,000
Month | Sales |
---|---|
January | $10,000 |
Tables in HTML are a fundamental way to display data on web pages. They allow you to represent information in rows and columns, with advanced features like spanning, nesting, and styling to enhance functionality and appearance. When used correctly, tables make data presentation clear and organized. Happy coding ! ❤️