How to create nest tables within tables in HTML?

Tables are a fundamental and critical aspect of web development and are used to present information in an ordered and legible format. However, some situations may necessitate presenting more intricate data that necessitates the use of nested tables. Nested tables are tables that are located within other table cells. In this article, we will direct you through the process of building nested tables in HTML, with a meticulous and detailed explanation accompanied by illustrations to help you comprehend the concept more effectively. Whether you're a neophyte or an experienced web designer, this article will furnish you with the knowledge and expertise required to create nested tables in HTML proficiently.

Before we commence our exploration of crafting nested tables, it is essential to comprehend the fundamental makeup of an HTML table. An HTML table is fashioned by the implementation of the <table> element and comprises one or more <tr> (table row) elements that embody <td> (table data cell) elements. Every <td> element denotes a solitary cell within the table.


The following approach shown here is used to create nested tables within tables. To do this, we first create the main table by enclosing the <table> element within the <table> element. The main table is defined with a class name "main-table". Inside the main table, we have two cells defined using <td> element. The first cell contains a nested table that is enclosed within another <table> element. The nested table is defined with a class name "nested-table". The cells of the nested table are defined using the <td> element. The second cell of the main table contains text but no nested tables.

To ensure that the tables are displayed properly, we have defined some styles using CSS. The table elements are set to have a width of 100% and a border-collapse value of collapse. The cells of both the main table and the nested table are given a black border of 1px and 8px of padding. The text alignment is set to left for all cells.

Additionally, we have defined background colors for the main table and the nested table using CSS. The main table has a light gray background color and the nested table has a slightly darker gray background color. By following these steps, you can easily create nested tables within tables in HTML and apply styles to them using CSS.


The following is the complete code which we are going to have a look at in this example −

<!DOCTYPE html>
   <title>How to create nested tables within tables in HTML?</title>
      table {
         border-collapse: collapse;
         width: 100%;
      td, th {
         border: 1px solid black;
         padding: 8px;
         text-align: left;
      .main-table {
         background-color: #f2f2f2;
      .nested-table {
         background-color: #e6e6e6;
   <h4>How to create nested tables within tables in HTML?</h4>
   <table class="main-table">
            Main table cell 1
            <table class="nested-table">
                  <td>Nested table cell 1</td>
                  <td>Nested table cell 2</td>
         <td>Main table cell 2</td>


In final analysis, generating embedded tables in HTML is a facile undertaking that necessitates a rudimentary grasp of HTML table composition. By adhering to the steps expounded in this article, you can readily produce embedded tables to present intricate data in a systematic and comprehensible way. Whether you require to present a voluminous amount of data or merely want to exhibit data in a precise layout, embedded tables are a pragmatic implement for any web designer. With the expertise garnered from this article, you now possess the competence to create embedded tables in HTML and elevate your web design to a higher echelon.

Updated on: 11-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started