How i make table inside table in html?

Learn how i make table inside table in html? with practical examples, diagrams, and best practices. Covers html, css development techniques with visual explanations.

Nesting Tables in HTML: Techniques and Best Practices

Hero image for How i make table inside table in html?

Explore various methods for embedding tables within tables in HTML, understanding their use cases, and learning best practices for accessibility and maintainability.

Nesting tables in HTML refers to the practice of placing one <table> element inside a cell (<td> or <th>) of another <table> element. While modern web development often favors CSS Grid or Flexbox for layout, nested tables can still be useful for specific data presentation scenarios, especially when dealing with complex, hierarchical data structures. This article will guide you through the process, discuss common pitfalls, and provide best practices to ensure your nested tables are both functional and accessible.

Basic Nested Table Structure

The fundamental concept of nesting tables is straightforward: you simply place a complete <table> tag, including its <thead>, <tbody>, <tr>, and <td> (or <th>) elements, inside a <td> or <th> element of an outer table. This allows you to create intricate data layouts where a single cell of the parent table can itself contain a structured grid of information.

<table border="1">
  <thead>
    <tr>
      <th>Outer Header 1</th>
      <th>Outer Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Outer Cell 1.1</td>
      <td>
        <!-- Nested Table Starts Here -->
        <table border="1">
          <thead>
            <tr>
              <th>Inner Header A</th>
              <th>Inner Header B</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Inner Cell A1</td>
              <td>Inner Cell B1</td>
            </tr>
            <tr>
              <td>Inner Cell A2</td>
              <td>Inner Cell B2</td>
            </tr>
          </tbody>
        </table>
        <!-- Nested Table Ends Here -->
      </td>
    </tr>
    <tr>
      <td>Outer Cell 2.1</td>
      <td>Outer Cell 2.2</td>
    </tr>
  </tbody>
</table>

A basic example of an HTML table with a nested table inside one of its cells.

Styling Nested Tables with CSS

Styling nested tables requires careful use of CSS selectors to target specific tables or cells. You can use descendant selectors to apply styles to inner tables without affecting outer ones, or vice-versa. It's crucial to avoid overly generic selectors that might unintentionally style both parent and child tables in undesirable ways.

/* Style for the outer table */
.outer-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

.outer-table th, .outer-table td {
  padding: 8px;
  border: 1px solid #ccc;
  text-align: left;
}

/* Style for the nested table */
.outer-table td > table {
  width: 100%; /* Make nested table fill its parent cell */
  border-collapse: collapse;
  margin: 0; /* Remove default margins */
}

.outer-table td > table th, .outer-table td > table td {
  padding: 5px;
  border: 1px solid #eee; /* Lighter border for inner table */
  background-color: #f9f9f9;
}

.outer-table td > table th {
  background-color: #e0e0e0;
}

CSS to style both outer and inner tables distinctly.

flowchart TD
    A[Outer Table] --> B{Outer Row}
    B --> C[Outer Cell 1]
    B --> D[Outer Cell 2]
    D --> E[Nested Table]
    E --> F{Nested Row}
    F --> G[Nested Cell A]
    F --> H[Nested Cell B]

Conceptual flow of nesting a table within an outer table's cell.

Accessibility Considerations for Nested Tables

While visually nesting tables might seem intuitive, it can pose significant challenges for screen readers and other assistive technologies. Screen readers typically navigate tables cell by cell, row by row. When a nested table is encountered, the context can become confusing. It's crucial to provide clear semantic markup and, if possible, use ARIA attributes to enhance accessibility.

<table border="1" summary="Summary of main product categories and their sub-products">
  <caption>Product Catalog</caption>
  <thead>
    <tr>
      <th scope="col">Category</th>
      <th scope="col">Details</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Electronics</td>
      <td>
        <table border="1" aria-labelledby="electronics-details">
          <caption id="electronics-details">Electronics Sub-Categories</caption>
          <thead>
            <tr>
              <th scope="col">Type</th>
              <th scope="col">Availability</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Smartphones</td>
              <td>In Stock</td>
            </tr>
            <tr>
              <td>Laptops</td>
              <td>Low Stock</td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
    <tr>
      <td>Apparel</td>
      <td>
        <table border="1" aria-labelledby="apparel-details">
          <caption id="apparel-details">Apparel Sub-Categories</caption>
          <thead>
            <tr>
              <th scope="col">Type</th>
              <th scope="col">Sizes</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Shirts</td>
              <td>S, M, L, XL</td>
            </tr>
            <tr>
              <td>Pants</td>
              <td>28-36</td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
  </tbody>
</table>

Example demonstrating improved accessibility for nested tables using <caption>, summary, scope, and aria-labelledby.

By using <caption> elements for both tables, summary for the outer table, scope="col" for headers, and aria-labelledby to link the inner table to its caption, you provide screen readers with better context and navigation cues. This helps users of assistive technologies understand the hierarchical relationship of the data.