Tables in HTML

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.

Introduction to HTML Tables

tables in html

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).

Example:

				
					<table>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Alice</td>
    <td>25</td>
  </tr>
  <tr>
    <td>Bob</td>
    <td>30</td>
  </tr>
</table>

				
			

Explanation:

  • The <table> element wraps the entire table.
  • Each <tr> (table row) contains table headers (<th>) or data cells (<td>).
  • The first row has two headers: “Name” and “Age”, followed by data rows with names and ages.
NameAge
Alice25
Bob30

Basic Table Structure

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).

Components of a Basic Table:

  1. Table element: The outer container (<table>).
  2. Rows: Each row is defined by the <tr> tag.
  3. Cells: Headers are created using <th>, and data is displayed using <td>.

Example:

				
					<table>
  <tr>
    <th>Product</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Apples</td>
    <td>$1.50</td>
  </tr>
  <tr>
    <td>Bananas</td>
    <td>$1.20</td>
  </tr>
</table>

				
			
ProductPrice
Apples$1.50
Bananas$1.20

Adding Table Headers

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.

Example:

				
					<table>
  <tr>
    <th>City</th>
    <th>Population</th>
  </tr>
  <tr>
    <td>New York</td>
    <td>8,398,748</td>
  </tr>
  <tr>
    <td>Los Angeles</td>
    <td>3,990,456</td>
  </tr>
</table>

				
			
CityPopulation
New York8,398,748
Los Angeles3,990,456

Headers are particularly useful when tables have a lot of data, ensuring the viewer knows what each column represents.

Spanning Columns and Rows

You can make a cell span across multiple columns or rows using the colspan and rowspan attributes. This is useful for merging cells.

Column Span (colspan):

				
					<table>
  <tr>
    <th colspan="2">Product Information</th>
  </tr>
  <tr>
    <td>Product</td>
    <td>Price</td>
  </tr>
  <tr>
    <td>Orange</td>
    <td>$0.75</td>
  </tr>
</table>

				
			
Product Information
Product
Orange

Here, the header spans two columns.

Row Span (rowspan):

				
					<table>
  <tr>
    <th>Day</th>
    <th>Event</th>
  </tr>
  <tr>
    <td rowspan="2">Monday</td>
    <td>Meeting</td>
  </tr>
  <tr>
    <td>Workshop</td>
  </tr>
</table>

				
			
DayEvent
MondayMeeting
Workshop

The “Monday” cell spans two rows.

Table Styling with CSS

Tables can be styled using CSS to improve readability and design. Common styles include borders, background colors, padding, and text alignment.

Example

				
					<style>table {
    width: 100%;
    border-collapse: collapse;
  }
  th, td {
    border: 1px solid black;
    padding: 8px;
    text-align: left;
  }
  th {
    background-color: #f2f2f2;
  }</style><table>
  <tr>
    <th>Item</th>
    <th>Quantity</th>
  </tr>
  <tr>
    <td>Bread</td>
    <td>2</td>
  </tr>
  <tr>
    <td>Milk</td>
    <td>1</td>
  </tr>
</table>

				
			

The border-collapse: collapse ensures that adjacent cell borders merge, and the background color for the header is light gray.

Nested Tables

You can create tables within tables to display complex hierarchical data. This is called nesting.

Example

				
					<table border="1">
  <tr>
    <th>Department</th>
    <th>Employees</th>
  </tr>
  <tr>
    <td>HR</td>
    <td>
      <table border="1">
        <tr><td>John</td></tr>
        <tr><td>Mary</td></tr>
      </table>
    </td>
  </tr>
</table>

				
			
DepartmentEmployees
HRJohn
Mary

The nested table is placed inside the HR department row, showing employees in that department.

Accessibility and Best Practices

To make your tables accessible, follow these best practices:

  • Use the <caption> element to add a title to the table.
  • Use the "scope" attribute in table headers to define their role (row or column header).
  • Avoid using tables for page layout, as they are meant for displaying tabular data.

Example

				
					<table>
  <caption>Monthly Sales Report</caption>
  <tr>
    <th scope="col">Month</th>
    <th scope="col">Sales</th>
  </tr>
  <tr>
    <th scope="row">January</th>
    <td>$10,000</td>
  </tr>
</table>

				
			
MonthSales
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 ! ❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India