What Is Table in HTML: Explained with Example

At the heart of webpage structure lies the humble yet powerful HTML table. If you're delving into the world of web development, understanding what is a table in HTML is elementary, and mastering it opens up a wealth of possibilities for presenting complex data in a digestible format.

HTML Table
HTML Table

Tables in HTML aren't just about displaying data in rows and columns; they're also the building blocks for complex page layouts, such as headers, navigation menus, and grids. Let's dive into the world of HTML tables, explore their use cases, and build an example to demonstrate their versatility.

HTML Basic Tables
HTML Basic Tables

Understanding HTML Tables

At their core, HTML tables are defined with the <table> tag and divided into rows using <tr>, with cells containing data demarcated by <td> (table data). Headers, on the other hand, use the <th> tag, which implies a column header and automatically centers the text within the cell.

HTML Tables Tags Tutorial with Example โ€” TutorialBrain
HTML Tables Tags Tutorial with Example โ€” TutorialBrain

The <thead> and <tbody> tags further organize tables by separating headers from the main body content. Plus, you can use <caption> to add a descriptive title for your table. Let's dive into specific use cases and see these elements in action.

Data Presentation

an image of what is a table in html?
an image of what is a table in html?

Tables' primary purpose is displaying tabular data. Consider a list of countries with their capitals:

<table> <caption>Countries and Capitals</caption> <thead> <tr> <th>Country</th> <th>Capital</th> </tr> </thead> <tbody> <tr> <td>USA</td> <td>Washington, D.C.</td> </tr> <tr> <td>Japan</td> <td>Tokyo</td> </tr> </tbody> </table>

Here, the <thead> and <tbody> tags separate headers from body content, making the table easier to read and manage.

Layout and Design

๐Ÿงพ How to Create Tables in HTML
๐Ÿงพ How to Create Tables in HTML

Beyond data, tables can construct complex webpage layouts. For instance, grid systems often rely on tables to create responsive rows and columns. Here's a simple 3-column grid layout:

<table> <tr> <td>Column 1</td> <td>Column 2</td> <td>Column 3</td> </tr> </table>

In this example, each <td> represents a column in the layout.

Advanced Table Features

Creating Tables in HTML- A Complete Guide.
Creating Tables in HTML- A Complete Guide.

HTML tables offer more advanced features like row and column spanning, to ensure data is presented logically and neatly. Let's explore these in our countries and capitals table.

Spanning Cells

an image of a table with many lines
an image of a table with many lines
Responsive HTML Table With Rounded Corners
Responsive HTML Table With Rounded Corners
tables in html table with text and images on the page, which is displayed below
tables in html table with text and images on the page, which is displayed below
Welcome to SiteRaw
Welcome to SiteRaw
๐Ÿ“Š Combine Table Cells in HTML
๐Ÿ“Š Combine Table Cells in HTML
an image of a text description with the words html tables in red and blue on it
an image of a text description with the words html tables in red and blue on it
HOW TO MERGE TABLE CELLS IN HTML
HOW TO MERGE TABLE CELLS IN HTML
How to Expand and Collapse Tables in HTML Using CSS
How to Expand and Collapse Tables in HTML Using CSS
the table borders for different types of text
the table borders for different types of text
Top 9 HTML Table Builders to use Free
Top 9 HTML Table Builders to use Free
A Detailed Guide to HTML Tables | Simplilearn
A Detailed Guide to HTML Tables | Simplilearn
Chapter 5: HTML โ€“ Create Lists and Tables
Chapter 5: HTML โ€“ Create Lists and Tables
an info sheet describing the different types of web pages and how they are used to create them
an info sheet describing the different types of web pages and how they are used to create them
an image of a table with numbers and symbols on the top right hand corner that says tables
an image of a table with numbers and symbols on the top right hand corner that says tables
Student Registration Form in HTML with CSS | Completely Free
Student Registration Form in HTML with CSS | Completely Free
an image of a computer screen with the text,'html tables'on it
an image of a computer screen with the text,'html tables'on it
CSS Table Layout
CSS Table Layout
Tables in HTML
Tables in HTML
a screenshot of a computer screen with the text'html tables'on it
a screenshot of a computer screen with the text'html tables'on it
The HTML Backbone
The HTML Backbone

We can extend our previous table example to include a new 'Continent' column. However, since most countries share the same continent as their capital, our table would look cluttered if we listed each one. Instead, we'll use the colspan attribute to span cells across multiple columns:

<table> <thead> <tr> <th>Country</th> <th>Continent</th> <th>Capital</th> </tr> </thead> <tbody> <tr> <td>USA</td> <td>North America</td> <td>Washington, D.C.</td> </tr> <tr> <td>Japan</td> <td colspan="2">Asia</td> </tr> </tbody> </table>

Now, 'Asia' takes up two columns in the row for Japan, making our table cleaner and more readable.

Table Styling

While we could leave our tables looking like classic spreadsheet software, it's essential to style them for a modern web design. You can use CSS to add colors, borders, and responsive columns. Here's a simple CSS example that improves our last table:

<style> table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid black; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } tr:nth-child(even) { background-color: #f9f9f9; } </style> <table> <thead> <tr> <th>Country</th> <th>Continent</th> <th>Capital</th> </tr> </thead> <tbody> ... (rest of the table) </tbody> </table>

Here, we use CSS to add border-collapse, borders, padding, text alignment, and background colors for alternate rows.

In conclusion, understanding and mastering HTML tables opens up a world of possibilities for structured data presentation and web design. Whether you're displaying information or crafting responsive layouts, tables are a cornerstone of HTML development. Happy (table) building!

Related Articles

Div Tag In Html Examples Pdf Html Table Basics Javascript Pdf Guide Div Tag Examples In Html Html Div Tag Class Write And Explain Table Tags In Html With Suitable Example Create Table In Html Code Example Greek Word For Greece Dart Methods Html Greek Symbols