Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is HTML Tables?
3.
HTML Table – Code Example
3.1.
HTML
4.
Tags Used in HTML Tables
5.
Adding a Border to an HTML Table
5.1.
HTML
6.
Adding Cell Padding in an HTML Table
6.1.
HTML
7.
Adding Left Align Headings in an HTML Table
7.1.
HTML
8.
Adding Border Spacing in an HTML Table
8.1.
HTML
9.
Adding Cells that Span Many Columns in HTML Tables
9.1.
HTML
10.
Adding a Background Colour to the Table
10.1.
HTML
11.
Creating Nested Tables
11.1.
HTML
12.
Frequently Asked Questions
12.1.
Can I use CSS instead of HTML attributes to style my tables?
12.2.
How can I make my HTML table responsive?
12.3.
Is it possible to add interactive elements to an HTML table?
12.4.
How to align tr and td in HTML?
13.
Conclusion
Last Updated: Oct 3, 2024
Easy

Table Tag in Html

Author Rahul Singh
0 upvote

Introduction

HTML tables are a fundamental part of web design, used to organize data into rows and columns. This structured format allows for better data presentation on websites, making it easier for users to read and understand information. 

Table Tag in Html

In this article, we'll learn about the basics of creating and manipulating tables in HTML. We'll look at different tags used to build tables, how to style them, and various techniques to enhance their appearance and functionality.

What is HTML Tables?

HTML tables are structures in web pages that organize text, images, and other data into rows and columns. Each table is created using HTML code, which includes specific tags that define the table’s layout. These tables are extremely useful when you need to align data systematically or want to display information side by side for comparison. For example, tables are often used to show schedules, price lists, or specifications of products in an organized manner.

HTML Table – Code Example

To create a basic HTML table, you start by using the <table> tag. This tag acts as a container for all your table content. Inside the <table>, you place <tr> tags to define each row of data. For each cell within a row, you use <td> tags. Here’s a simple example to show how these tags work together:

  • HTML

HTML

<table>

   <tr>

       <td>Name</td>

       <td>Age</td>

       <td>City</td>

   </tr>

   <tr>

       <td>Neha</td>

       <td>23</td>

       <td>New York</td>

   </tr>

   <tr>

       <td>Riya</td>

       <td>22</td>

       <td>Los Angeles</td>

   </tr>

</table>

Output

Output

In this example, the <table> tag starts the table. Each <tr> defines a new row, and each <td> within the <tr> represents a cell in that row. The first row often contains headers to label the columns, which can alternatively be marked using <th> tags for semantic clarity.

Tags Used in HTML Tables

Let's look at a few tags used in HTML Tables:

TagDescription
<table>Defines a table in an HTML document. It is used as a container for the table content, including rows and cells.
<tr>Defines a row in a table. It is used to group and contain the table cells (<td> or <th>) that make up a single row.
<th>Defines a header cell in a table. It is used to specify the header content of a column or row. Header cells are typically displayed in bold and centered by default.
<td>Defines a standard data cell in a table. It is used to contain the actual data or content within a table row.
<caption>Defines a caption or title for a table. It is typically displayed above the table and provides a brief description or summary of the table's content.
<colgroup>Specifies a group of one or more columns in a table for formatting purposes. It allows you to apply styles or properties to a specific group of columns without affecting the entire table.
<col>Specifies column properties for each column within a <colgroup> element. It is used to define the width, style, or other attributes for one or more columns.
<thead>Groups the header content in a table. It is used to define the table's header rows and is typically rendered at the top of the table, remaining visible even when the table's body is scrolled.
<tbody>Groups the main content or data rows in a table. It is used to encapsulate the table's body, which contains the majority of the table's data.
<tfoot>Groups the footer content in a table. It is used to define the table's footer rows and is typically rendered at the bottom of the table. The footer usually contains summary information or totals for the data.

Adding a Border to an HTML Table

To make an HTML table more visually appealing and easier to read, adding a border is a common practice. You can add a border to a table by using the border attribute in the <table> tag. For example, if you want a simple border around each cell and the entire table, you can set the border attribute to a specific pixel width.

Let’s see how we can do it : 

  • HTML

HTML

<table border="1">

   <tr>

       <th>Name</th>

       <th>Age</th>

       <th>City</th>

   </tr>

   <tr>

       <td>Neha</td>

       <td>23</td>

       <td>New York</td>

   </tr>

   <tr>

       <td>Riya</td>

       <td>22</td>

       <td>Los Angeles</td>

   </tr>

</table>

Output

Output

In this code, border="1" adds a thin border with a width of 1 pixel around each cell and the outer edges of the table. This simple addition not only defines the boundaries of each cell clearly but also gives the entire table a structured and tidy appearance.

Adding Cell Padding in an HTML Table

Cell padding is a feature that adds space between the cell content and the cell borders, making the data inside the table easier to read. To add cell padding to an HTML table, you use the cellpadding attribute in the <table> tag. This attribute specifies the amount of space, in pixels, between the content of a cell and its border.

Here’s how you can add cell padding to your table:

  • HTML

HTML

<table border="1" cellpadding="5">

   <tr>

       <th>Name</th>

       <th>Age</th>

       <th>City</th>

   </tr>

   <tr>

       <td>Neha</td>

       <td>23</td>

       <td>New York</td>

   </tr>

   <tr>

       <td>Riya</td>

       <td>22</td>

       <td>Los Angeles</td>

   </tr>

</table>

Output

Output

In this example, cellpadding="5" adds 5 pixels of padding inside each cell. This padding ensures that the text does not touch the cell borders, which enhances the overall readability of the table. The content within the cells has room to breathe, making the table look neater and more organized.

Adding Left Align Headings in an HTML Table

Aligning table headings to the left is a straightforward way to improve the readability of your HTML table, especially when dealing with text-heavy data. To align headings to the left, you use the align attribute within the <th> tag. This attribute can set the alignment of the text within the headers, and specifying "left" will align your headings to the left side of each column.

For example : 

  • HTML

HTML

<table border="1" cellpadding="5">

   <tr>

       <th align="left">Name</th>

       <th align="left">Age</th>

       <th align="left">City</th>

   </tr>

   <tr>

       <td>Neha</td>

       <td>23</td>

       <td>New York</td>

   </tr>

   <tr>

       <td>Riya</td>

       <td>22</td>

       <td>Los Angeles</td>

   </tr>

</table>

Output

Output

In this code snippet, the align="left" attribute in each <th> tag ensures that the headings for "Name," "Age," and "City" are aligned to the left side of their respective columns. This alignment makes the table look tidy and organized, providing a clean visual guide for readers when they will scroll and read the information.

Adding Border Spacing in an HTML Table

Border spacing in an HTML table refers to the space between the borders of adjacent cells. Adjusting this spacing can help make the table more readable and visually appealing, especially when the table contains a lot of data. To control the spacing between cell borders, you use the border-spacing CSS property on the <table> element.

For example : 

  • HTML

HTML

<table style="border-collapse: separate; border-spacing: 10px;">

   <tr>

       <th align="left">Name</th>

       <th align="left">Age</th>

       <th align="left">City</th>

   </tr>

   <tr>

       <td>Neha</td>

       <td>23</td>

       <td>New York</td>

   </tr>

   <tr>

       <td>Riya</td>

       <td>22</td>

       <td>Los Angeles</td>

   </tr>

</table>

 

Output

Output

In this code, border-spacing: 10px; sets the space between the borders of adjacent cells to 10 pixels. Additionally, the border-collapse: separate; property is important because it ensures that the border-spacing property works correctly. Without setting border-collapse to separate, the default behavior is for table borders to be merged, which would ignore the border-spacing setting.

Adding Cells that Span Many Columns in HTML Tables

Sometimes, you might need a cell in an HTML table to span across multiple columns. This is particularly useful when you want to emphasize a heading or a specific piece of information that applies to more than one column. To achieve this, you use the colspan attribute in the <td> or <th> tag.

For example : 

  • HTML

HTML

<table border="1" cellpadding="5" style="border-spacing: 10px;">

   <tr>

       <th align="left">Name</th>

       <th align="left">Age</th>

       <th align="left">City</th>

   </tr>

   <tr>

       <td colspan="3" align="center">Important Notice: All information is confidential</td>

   </tr>

   <tr>

       <td>Neha</td>

       <td>23</td>

       <td>New York</td>

   </tr>

   <tr>

       <td>Riya</td>

       <td>22</td>

       <td>Los Angeles</td>

   </tr>

</table>

Output

Output

In this example, the <td> tag with colspan="3" extends across all three columns, providing a single, large cell that can be used for a title, a notice, or any other information that benefits from this format. The align="center" attribute centers the text within this extended cell, highlighting its importance or distinguishing it from other rows.

Adding a Background Colour to the Table

Adding a background color to your HTML table can significantly improve its visibility and make it more attractive. This is particularly useful when you want to draw attention to specific parts of your table or simply want to enhance the aesthetic appeal of your webpage. You can add background color to the entire table or to specific cells or rows using the style attribute with the background-color property.

For example : 

  • HTML

HTML

<table border="1" cellpadding="5" style="background-color: #f8f8f8;">

   <tr>

       <th align="left">Name</th>

       <th align="left">Age</th>

       <th align="left">City</th>

   </tr>

   <tr>

       <td>Neha</td>

       <td>23</td>

       <td>New York</td>

   </tr>

   <tr>

       <td>Riya</td>

       <td>22</td>

       <td>Los Angeles</td>

   </tr>

</table>

Output

Output


In this code, style="background-color: #f8f8f8;" applies a light grey background color to the entire table. This color helps the text stand out more clearly, enhancing readability. You can choose any color that fits your design needs by modifying the hex code in the background-color property.

Creating Nested Tables

Nested tables are essentially tables within tables, a layout technique used to organize complex sets of data more effectively. This method can be particularly helpful when you need to associate specific data points closely with a subset of information in your web design.

Here’s how you can create a nested table:

  • HTML

HTML

<table border="1" cellpadding="5" style="background-color: #f8f8f8;">

   <tr>

       <th align="left">Name</th>

       <th align="left">Details</th>

   </tr>

   <tr>

       <td>Neha</td>

       <td>

           <table border="1" style="background-color: #e0e0e0;">

               <tr>

                   <td>Age: 23</td>

                   <td>City: New York</td>

               </tr>

           </table>

       </td>

   </tr>

   <tr>

       <td>Riya</td>

       <td>

           <table border="1" style="background-color: #e0e0e0;">

               <tr>

                   <td>Age: 22</td>

                   <td>City: Los Angeles</td>

               </tr>

           </table>

       </td>

   </tr>

</table>

Output

Output


In this example, each 'Details' cell contains another table, which itself includes more specific information about the individual—such as their age and city. This nested structure allows for a clear and organized display of related data points that belong under a broader category, in this case, under a person's name.

Nested tables can make the presentation of detailed data more informative and visually structured, but it’s important to use them cautously to avoid complicating the webpage layout unnecessarily.

Frequently Asked Questions

Can I use CSS instead of HTML attributes to style my tables?

Yes, it's recommended to use CSS for styling tables as it provides greater flexibility and control over the presentation. CSS can be used to apply styles globally and adjust layout properties that are not possible with HTML attributes alone.

How can I make my HTML table responsive?

To make an HTML table responsive, you can use CSS techniques such as the overflow property, wrapping the table in a container with overflow-x: auto;. This allows the table to scroll horizontally on smaller screens.

Is it possible to add interactive elements to an HTML table?

Yes, you can add interactive elements such as buttons, links, and form inputs within your table cells. This allows users to perform actions like submitting data or navigating to other pages directly from the table.

How to align tr and td in HTML?

To align content in <tr> and <td> elements in HTML, use the align attribute directly on the <td> or <th> tags or use CSS properties like text-align for horizontal alignment and vertical-align for vertical positioning.

Conclusion

In this article, we have learned how to effectively create and manage HTML tables. We've discussed everything from the basics of setting up a table to adding styles that enhance readability and functionality with proper examples. We explained tags, alignment, coloring, and even advanced topics like nested tables and responsiveness. With the help of these attributes, you can make your webpage more visually appealing and attractive.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass