HTML tables are used to display information in a structured format using rows and columns. Tables help organize large amounts of data and make it easier for users to read and compare information.
Tables are commonly used on websites for displaying product details, student records, price lists, schedules, comparison charts, and other tabular information.
HTML provides different tags to create tables, define rows, add columns, and organize data properly.
Understanding HTML tables is an important skill for beginners because structured data presentation is required in many real-world websites.
An HTML table is a collection of rows and columns used to display data in a grid-like structure.
Each table contains horizontal sections called rows and vertical sections called columns. The intersection of a row and column creates a cell where data is stored.
HTML tables provide a simple method to arrange related information in a readable format.
An HTML table is created using the <table> tag. Inside the table element, rows and cells are created using different tags.
| Tag | Purpose |
|---|---|
| <table> | Creates an HTML table. |
| <tr> | Creates a table row. |
| <th> | Creates a table heading cell. |
| <td> | Creates a table data cell. |
The table tag is the main container used to create a table in HTML.
All rows, headings, and data cells are written inside the table element.
<table> Table Content </table>
<table> </table>
The table tag itself does not display data. Rows and cells must be added inside it.
The tr tag is used to create a row inside an HTML table.
Each row contains one or more heading cells or data cells.
<tr> Table Row Content </tr>
<tr> <td> HTML </td> <td> Web Development </td> </tr>
A table can contain multiple rows depending on the amount of information.
The th tag is used to create heading cells in a table.
By default, table headings appear bold and centered in most browsers.
<th> Heading Name </th>
<th> Student Name </th>
The td tag is used to add actual data inside a table.
Each table data cell represents one piece of information.
<td> Data </td>
<td> Rahul </td>
Multiple td elements are placed inside a row to create columns.
<table> <tr> <th> Name </th> <th> Subject </th> </tr> <tr> <td> Rahul </td> <td> HTML </td> </tr> </table>
<table border="1"> <tr> <th> Name </th> <th> Course </th> </tr> <tr> <td> Amit </td> <td> B.Tech CS </td> </tr> <tr> <td> Neha </td> <td> BCA </td> </tr> </table>
| Name | Course |
|---|---|
| Amit | B.Tech CS |
| Neha | BCA |
The border attribute is used to display borders around table cells.
<table border="1"> Table Content </table>
<table border="1"> <tr> <td> HTML </td> <td> CSS </td> </tr> </table>
In modern web development, CSS is preferred for styling table borders.
Rows and columns are the basic building blocks of every HTML table.
Rows are horizontal sections of a table created using the tr tag.
Columns are vertical sections created by placing multiple cells in each row.
<table border="1"> <tr> <td> HTML </td> <td> CSS </td> </tr> </table>
This table contains one row and two columns.
HTML tables are powerful elements used to display structured information on webpages. The table tag creates the structure, tr creates rows, th creates headings, and td stores data.
In this part, we learned the basic concepts of HTML tables, table tags, rows, columns, headers, data cells, syntax, and examples.
The caption tag is used to provide a title or description for an HTML table. It helps users understand the purpose of the table before reading the data.
The caption is displayed above the table by default.
<table> <caption> Table Title </caption> Table Content </table>
<table border="1"> <caption> Student Details </caption> <tr> <th> Name </th> <th> Marks </th> </tr> <tr> <td> Rahul </td> <td> 85 </td> </tr> </table>
| Name | Marks |
|---|---|
| Rahul | 85 |
HTML provides special tags to divide a table into different sections. These tags improve table organization and readability.
| Tag | Purpose |
|---|---|
| <thead> | Defines the header section of a table. |
| <tbody> | Defines the main data section. |
| <tfoot> | Defines the footer section of a table. |
The thead tag contains table heading rows. It is mainly used to separate column titles from table data.
<table border="1"> <thead> <tr> <th> Subject </th> <th> Marks </th> </tr> </thead> </table>
The tbody tag contains the main data rows of a table.
<tbody> <tr> <td> HTML </td> <td> 90 </td> </tr> </tbody>
The tfoot tag represents the summary or final section of a table.
It is commonly used for totals, calculations, or additional information.
<tfoot> <tr> <td> Total </td> <td> 90 </td> </tr> </tfoot>
<table border="1"> <caption> Course Details </caption> <thead> <tr> <th> Course </th> <th> Duration </th> </tr> </thead> <tbody> <tr> <td> HTML </td> <td> 3 Months </td> </tr> </tbody> <tfoot> <tr> <td> Total </td> <td> 1 Course </td> </tr> </tfoot> </table>
Cell padding defines the space between the content and the border of a table cell.
It improves readability by creating extra space inside cells.
<table cellpadding="10"> Table Content </table>
<table border="1" cellpadding="10"> <tr> <td> HTML </td> <td> CSS </td> </tr> </table>
A higher cellpadding value increases the space inside each cell.
Cell spacing controls the distance between individual table cells.
<table cellspacing="5"> Table Content </table>
<table border="1" cellspacing="5"> <tr> <td> HTML </td> <td> CSS </td> </tr> </table>
In modern websites, CSS border-spacing is preferred instead of the cellspacing attribute.
HTML allows developers to define table size using width and height attributes.
<table width="100%" height="200"> Table Content </table>
<table border="1" width="80%"> <tr> <td> HTML Table </td> </tr> </table>
Today, CSS is recommended for controlling table dimensions.
The rowspan attribute is used to merge two or more rows into a single cell.
<td rowspan="number"> Content </td>
<table border="1"> <tr> <td rowspan="2"> HTML </td> <td> Basic </td> </tr> <tr> <td> Advanced </td> </tr> </table>
| HTML | Basic |
| Advanced |
The colspan attribute is used to merge two or more columns into a single cell.
<td colspan="number"> Content </td>
<table border="1"> <tr> <td colspan="2"> Computer Science </td> </tr> </table>
| Computer Science |
In this part, we learned advanced HTML table concepts including captions, table sections, cell spacing, cell padding, table size, rowspan, and colspan attributes.
These features help developers create organized and professional data layouts for websites.
HTML creates the basic structure of tables, but CSS is used to improve the appearance and design of tables. CSS helps developers control borders, colors, spacing, alignment, and responsive behavior.
Modern websites generally use CSS instead of old HTML attributes for designing tables.
<style>
table{
width:100%;
border-collapse:collapse;
}
th,td{
border:1px solid black;
padding:10px;
text-align:center;
}
</style>
<table>
<tr>
<th>
Name
</th>
<th>
Course
</th>
</tr>
<tr>
<td>
Rahul
</td>
<td>
B.Tech
</td>
</tr>
</table>
| Name | Course |
|---|---|
| Rahul | B.Tech |
The border-collapse property is used to combine table borders into a single border. It removes the extra space between table cells.
table{
border-collapse:collapse;
}
<style>
table{
border-collapse:collapse;
}
</style>
This property creates a cleaner and professional table design.
The text-align property is used to control the horizontal alignment of table content.
td{
text-align:center;
}
CSS can be used to add background colors to table headings and rows to make important information more visible.
th{
background-color:lightgray;
}
td{
background-color:white;
}
Proper styling improves user experience and makes tables easier to understand.
The hover effect highlights a table row when the user moves the mouse pointer over it.
tr:hover{
background-color:#eeeeee;
}
Hover effects are commonly used in data tables, dashboards, and admin panels.
A responsive table automatically adjusts according to different screen sizes such as mobile phones, tablets, and desktops.
Large tables may not fit properly on small screens. A responsive design allows users to scroll horizontally when required.
<div style="overflow-x:auto;"> <table border="1"> <tr> <th> Name </th> <th> Department </th> </tr> <tr> <td> Amit </td> <td> Computer Science </td> </tr> </table> </div>
The overflow-x property helps tables work properly on smaller devices.
A nested table is a table placed inside another table cell. It is useful when complex information needs multiple levels of organization.
<table>
<tr>
<td>
<table>
Inner Table
</table>
</td>
</tr>
</table>
<table border="1"> <tr> <td> Main Table <table border="1"> <tr> <td> Nested Table </td> </tr> </table> </td> </tr> </table>
Nested tables should be used carefully because excessive nesting can make HTML structure difficult to maintain.
HTML tables are used in many real-world applications for displaying structured information.
<table border="1"> <tr> <th> Name </th> <th> Subject </th> <th> Marks </th> </tr> <tr> <td> Amit </td> <td> HTML </td> <td> 90 </td> </tr> <tr> <td> Neha </td> <td> CSS </td> <td> 85 </td> </tr> </table>
| Name | Subject | Marks |
|---|---|---|
| Amit | HTML | 90 |
| Neha | CSS | 85 |
Accessibility helps all users, including users who use screen readers, understand table information properly.
<th scope="col"> Name </th>
| Element / Attribute | Purpose |
|---|---|
| <table> | Creates table structure. |
| <tr> | Creates table rows. |
| <th> | Creates table headings. |
| <td> | Creates table data cells. |
| rowspan | Merges multiple rows. |
| colspan | Merges multiple columns. |
| caption | Adds table title. |
| thead, tbody, tfoot | Organizes table sections. |
HTML tables are important elements for presenting structured information on webpages. They allow developers to arrange data into rows and columns in an organized manner.
By using table tags, attributes, and CSS styling techniques, developers can create professional, responsive, and user-friendly data layouts.
A good understanding of HTML tables helps beginners build better websites and prepares them for advanced web development concepts.