Category: Tables

  • Nested Tables

    Nested Tables

    HTML nested tables refer to the table where one or more tables are placed inside it; the inner tables are placed inside the <td> tag. The nested tables involve the utilization of one table within another, providing a versatile way to structure complex data layouts. Various elements, including other HTML tags, can be incorporated within table cells (<td>).

    Not only can tables be nested, but various HTML tags can also be used within the <td> (table data) tag for arranging contents in a structured manner.

    Basic Structure of Nested Tables

    Nested tables refer to the practice of embedding an entire table structure within a cell of another table. This technique allows for the creation of more complex and structured layouts in HTML.

    See this image of how a nested table looks like i.e., this image demonstartes a structure of nested tables:

    HTML Nested Tables

    How To Create a Nested Table?

    You can create a nested table in HTML by creating one or more tables in different table cells. The following are the steps to create an HTML nested table:

    Step 1: Create Outer Table

    Begin by creating the outer table, which serves as the container.

    <table border="1"><!-- Outer table content --></table>

    Step 2: Define Rows and Columns

    Within the outer table, define the necessary rows and columns.

    <tr><td><!-- Content within the cell --></td></tr>

    Step 3: Embed Inner Table

    Inside the cell, embed a new table structure using the <table> tags.

    <td><table border="1"><!-- Inner table content --></table></td>

    Step 4: Define Inner Table Content

    Within the inner table, define rows and columns as needed.

    <tr><td>Inner Content</td></tr>

    Step 5 – Close Tags

    Ensure proper closure of all HTML tags.

    </table>

    Example of Nested Tables

    Following is an example by following the above steps −

    <!DOCTYPE html><html><head><title>Nested Tables</title></head><body><table border="1" width="100%"><tr><td><table border="1" width="100%"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></td></tr></table></body></html>

    Tables Within Cells

    Tables can be nested within table cells, allowing for complex structures.

    Example

    The following example creates a table with two columns (Name and Salary). Inside the first column, there’s a nested table displaying employee details with the same columns.

    The outer table’s header and data rows are defined, and the inner table showcases two employees, Ramesh Raman, and Shabbir Hussein, with corresponding salaries.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td><table border="1" width="100%"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></td></tr></table></body></html>

    Styling Nested Tables

    You can apply styles to nested tables just like regular tables to enhance visual appearance. To write CSS styles for nested tables, you can simply apply the CSS rules on the ‘table‘ selector to style the outer table and use the ‘table table‘ selector to style the inner tables.

    Example

    The following code includes a main table styled with CSS properties. The <a href="https://www.tutorialspoint.com/css/css_border-collapse.htm" target="_blank" rel="noreferrer noopener">border-collapse</a> ensures seamless borders, and ‘width: 100%’ makes it responsive. Cells (<td><th>) have padding, border, and text alignment.

    Additionally, nested tables within cells inherit a distinctive style, including a colored background and borders. The main table’s data cell contains a nested table with a blue border, 80% width, and centered using ‘margin: 10px auto’.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Nested Tables with CSS Styles</title><style>
    
      table {
         border-collapse: collapse;
         width: 100%;
      }
      td,
      th {
         border: 1px solid #dddddd;
         text-align: left;
         padding: 8px;
      }
      /* Additional styles for nested tables */
      table table {
         border: 2px solid #3498db;
         width: 80%;
         margin: 10px auto;
      }
      table table td {
         background-color: #ecf0f1;
         border: 1px solid #bdc3c7;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td><table border="1" width="100%"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></td></tr></table></body></html>

    Images in Nested Tables

    Embedding images within nested tables enhances visual presentation. Use the <img> tag inside table cells to seamlessly integrate images into complex layouts like nested tables.

    Example

    The following example illustrates the use of images inside nested tables:

    <!DOCTYPE html><html><head><title>Nested Tables</title></head><body><table border="1" width="100%"><tr><td><table border="1" width="100%"><tr><th>Image </th><th>Name</th></tr><tr><td><img src="images\logo.png" alt="Nested Image"></td><td>LOGO </td></tr><tr><td><img src="images\html5_icon.png" alt="Nested Image"></td><td>HTML5 </td></tr></table></td></tr></table></body></html>

    Benefits of Nested Tables

    Following are the advantages of the nested tables −

    • Organized Layouts − Nested tables enable the creation of organized and structured layouts, enhancing the presentation of complex data.
    • Cell Customization − Each cell in a nested table can contain diverse content, including text, images, or even additional nested tables.
    • Complex Designs − Achieve intricate design patterns by nesting tables within cells, providing flexibility in webpage design.
  • Table Colgroup

    HTML Table Colgroup

    In HTML, the <colgroup> element is used to define a group of columns in a table. It allows you to apply properties to multiple columns simultaneously, providing a more efficient way to style or format columns.

    HTML Table Colgroup

    The <colgroup> Tag

    The <colgroup> is often used in conjunction with the <col> element, where each <col> tag represents an individual column within the group. This grouping enhances readability and simplifies the application of styles or attributes to specific columns in a table.

    Syntax

    Following is the syntax to use <colgroup> with <table> tag:

    <table><colgroup><col span="value" style="width: ...;"><col style="..."><!-- More <col> elements... --></colgroup><!-- Other table elements such as <thead>, <tbody>, ... --></table>

    Using <colgroup> Tag in HTML Table

    Using <colgroup> in HTML involves the following steps −

    1. Insert <colgroup> Tag

    Place the <colgroup> tag within the <table> element, usually inside the <thead> (table head) or <tbody> (table body) section.

    <table><colgroup><!-- Column definitions --></colgroup><thead><!-- Table headers --></thead><tbody><!-- Table rows --></tbody></table>

    2. Define Columns

    Inside the <colgroup> tag, use one or more <col> tags to represent each column. Specify attributes or styles for the columns within these <col> tags.

    <table><colgroup><col><col><col></colgroup><!-- Table content --></table>

    3. Apply Attributes or Styles

    Define attributes or styles for the columns by adding attributes such as spanwidthstyle, or class to the <col> tags.

    <table><colgroup><col style="background-color: lightgrey;" span="2"><!-- First two columns --><col style="background-color: lightblue;"><!-- Third column --></colgroup><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td></tr></tbody></table>

    Example of HTML Table Colgroup

    In this example, the <colgroup> tag defines two columns with different widths, and the styles are applied to the columns using the &lt;col&gt; tags. The second row in the table is highlighted using a CSS class.

    <!DOCTYPE html><html><body><table border=1><colgroup><col style="width: 30%;"><col style="width: 70%;"></colgroup><thead><tr><th>Column 1</th><th>Column 2</th></tr></thead><tbody><tr><td>Row 1, Col 1</td><td>Row 1, Col 2</td></tr><tr class="highlight"><td>Row 2, Col 1</td><td>Row 2, Col 2</td></tr></tbody></table></body></html>

    CSS Properties for <colgroup> Tag

    In HTML, the <colgroup> element allows some specific CSS properties to enhance the presentation of table columns. The legal CSS properties that can be used within a <colgroup> are as follows −

    • width Property − This property sets the width of the columns within the <colgroup>. It allows you to define the relative or absolute width of each column.
    • visibility Property − The visibility property can be used to control the visibility of columns within the <colgroup>. You can set it to “hidden” to make a column invisible.
    • Background Properties − Background properties, such as background-color, can be applied to add background styling to the columns. This can enhance the visual appeal of the table.
    • Border Properties − Border properties, like border-color and border-width, enable the customization of borders around the columns. This is useful for creating well-defined visual boundaries.

    Attempting to apply other CSS properties will have no impact on the styling of the table columns. Therefore, when styling tables with <colgroup>, focus on the available properties to achieve the desired layout and appearance.

    Example

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>
    
      table {
         width: 100%;
         border-collapse: collapse;
      }
      colgroup {
         /* Setting width for columns */
         width: 20%;
         background-color: #f0f0f0;
         /* Background color for columns */
         visibility: visible;
         /* Making columns visible */
         border: 2px solid #3498db;
         /* Border around columns */
      }
      col {
         /* Additional styling for individual columns */
         background-color: #ecf0f1;
         border: 1px solid #bdc3c7;
      }
      td,
      th {
         border: 1px solid #dddddd;
         text-align: left;
         padding: 8px;
      }
    </style></head><body><table><colgroup><col><col style="width: 30%;"><col></colgroup><thead><tr><th>Header 1</th><th>Header 2</th><th>Header 3</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td></tr><tr><td>Data 4</td><td>Data 5</td><td>Data 6</td></tr></tbody></table></body></html>

    Multiple Col Elements

    Certainly! The <colgroup> element in HTML allows you to group a set of columns in a table and apply styles to them collectively. Within <colgroup>, you can use multiple <col> elements to define different styles for individual columns.

    Example

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>
    
      col {
         /* Additional styling for individual columns */
         background-color: #ecf0f1;
         border: 1px solid #bdc3c7;
      }
    </style></head><body><table border=5><colgroup><col style="width: 20%;"><col style="width: 30%;"><col style="width: 50%;"></colgroup><thead><tr><th>Header 1</th><th>Header 2</th><th>Header 3</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td></tr><tr><td>Data 4</td><td>Data 5</td><td>Data 6</td></tr></tbody></table></body></html>

    The <colgroup> contains three <col> elements, each with a specific ‘width’ style, defining the width of individual columns.

    Empty Column groups

    In HTML, a <colgroup> element can be used to define a group of columns in a table. An empty <colgroup> can be employed to provide a structural placeholder for potential styling or later use. While it doesn’t contain explicit <col> elements, it can still influence the overall structure of the table.

    Example

    Here’s a simple example demonstrating the use of an empty <colgroup>. In here, the <colgroup> is empty but serves as a placeholder for potential styling. The entire <colgroup> is styled with a background color and a border. The <col> elements are not explicitly used, but their styling can be defined within <colgroup> for future use or consistency in the structure.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>
    
      colgroup {
         /* Styling for the colgroup (can be empty) */
         background-color: #f0f0f0;
         /* Background color for the entire colgroup */
         border: 2px solid #3498db;
         /* Border around the entire colgroup */
      }
      /* Additional styling for individual columns */
      col {
         background-color: #ecf0f1;
         border: 1px solid #bdc3c7;
      }
    </style></head><body><table border=3><colgroup></colgroup><thead><tr><th>Header 1</th><th>Header 2</th><th>Header 3</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td><td>Data 3</td></tr><tr><td>Data 4</td><td>Data 5</td><td>Data 6</td></tr></tbody></table></body></html>
  • Table Styling

    You can style HTML tables by using the CSS. Table styling helps you to customize the normal appearance of the elements like borders, cell padding, text alignment, background colors, and more to create a well-formatted table on a webpage.

    The following are some of the table stylings in HTML:

    • Collapsed Border Table
    • Horizontal Zebra Striped Table
    • Text Alignment in Table Cells
    • Vertical Zebra Striped Table
    • Table with Combined Vertical and Horizontal Zebra Stripes
    • Table with Horizontal Dividers
    • Hoverable Table Rows

    Collapsed Border Table

    You have the flexibility to manage the space between table borders by manipulating the ‘border-collapse’ property. This property determines how adjacent table cell borders interact, and adjusting it allows you to control the spacing or gap between the borders within your table.

    By setting ‘border-collapse’ to “collapse”, borders will merge, eliminating any spacing, while “separate” allows you to control the spacing using the ‘border-spacing’ property, providing a more customized appearance to your table layout.

    Example

    This is an example of a collapsed border table, where the table borders are merged into a single border using the border-collapse: collapse; property.

    <!DOCTYPE html><html><head><style>
    
      #table1 {
         border-collapse: separate;
      }
      #table2 {
         border-collapse: collapse;
      }
    </style></head><body><table id="table1" border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table><br /><table id="table2" border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Horizontal Zebra Striped Table

    The Zebra Stripes – Horizontal technique involves styling table rows with alternating colors, enhancing the visual appeal and readability of the table.

    The :nth-child(even) selector targets every second row, and a background color is applied to create a striped effect.

    Example

    This is an example of a horizontal zebra-striped table, here alternating rows are styled with different background colors:

    <!DOCTYPE html><html><head><style>
    
      tr:nth-child(even) {
         background-color: #8a83de;
      }
    </style></head><body><table border="1"><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></table></body></html>

    Text Alignment in Table Cells

    You can align the text within your table cells horizontally and vertically using the text-align and vertical-align properties.

    Example

    This is an example of text alignment in table cells:

    <!DOCTYPE html><html><head><style>
    
      td,
      th {
         text-align: center;
         vertical-align: middle;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Vertical Zebra Striped Table

    The Zebra Stripes – Vertical technique enhances table readability by applying alternating styles to every other column. This is achieved using the :nth-child(even) selector for both table data (td) and table header (th) elements.

    Example

    This is an example of a vertical zebra-striped table, where alternating columns are styled with different background colors:

    <!DOCTYPE html><html><head><style>
    
      td:nth-child(even),
      th:nth-child(even) {
         background-color: #D6EEEE;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Table with Combined Vertical & Horizontal Zebra Stripes

    You can achieve a captivating visual effect by combining both horizontal and vertical zebra stripe patterns on your table. This involves applying alternating styles to both rows (:nth-child(even)) and columns (td:nth-child(even), th:nth-child(even)).

    To enhance this effect, consider adjusting the color transparency using the rgba() function, creating an engaging and aesthetically pleasing overlap of stripes for a unique and visually interesting outcome.

    Example

    This is an example of a table with combined vertical and horizontal zebra stripes, where both alternating rows and columns are styled with different background colors:

    <!DOCTYPE html><html><head><style>
    
      tr:nth-child(even) {
         background-color: rgba(150, 212, 212, 0.4);
      }
      th:nth-child(even),
      td:nth-child(even) {
         background-color: rgba(212, 150, 192, 0.4);
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Table with Horizontal Dividers

    You can enhance the visual structure of your table by incorporating horizontal dividers. Achieve this effect by styling each ‘<tr>’ element with a bottom border.

    This CSS customization provides a clear separation between rows, contributing to improved table clarity and a more organized presentation of tabular data.

    Example

    This is an example of a table with horizontal dividers, where each row is separated by a distinct horizontal line:

    <!DOCTYPE html><html><head><style>
    
      table {
         border-collapse: collapse;
      }
      tr {
         border-bottom: 5px solid #ddd;
      }
      th,
      td {
         padding: 10px;
         /* Add padding for better visibility */
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    The above HTML program defines a simple table with two rows and two columns. CSS styles are applied to create a visual separation between rows using a solid border at the bottom of each row. The border-collapse property ensures a cleaner layout, and padding is added for improved visibility of table cells.

    Hoverable Table Rows

    You can improve user interaction by employing the ‘:hover’ selector, which highlights table rows when users hover over them. This enhances the visual feedback, making the table more interactive and user-friendly.

    Example

    This is an example of hoverable table rows, where the background color of a table row changes when the user hovers over it:

    <!DOCTYPE html><html><head><style>
    
      tr:hover {
         background-color: #D6EEEE;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    The above HTML program creates a table with a border. The CSS style makes the rows change the background color to light blue when hovered over, enhancing user interaction.

  • Table Headers and Captions

    Table Headers and Captions

    Headers and captions are used inside tables to organize and present data in a structured format.

    The table heading is an essential part of a table, providing labels for columns. The <th> (table header) element is used to define table headings.

    Captions are used in the tables to provide a title or explanation for the table. The <caption> element is placed immediately after the opening table tag.

    HTML Table Headers and Captions

    The <caption> tag is deprecated in HTML5 and XHTML. This means that it is still supported by most web browsers, but it is not recommended for use in new web pages. If you are writing new code, you should use the figure and figcaption elements instead. The figure element is used to group related content, and the figcaption element is used to provide a caption for the content.

    The <caption> tag is deprecated in HTML5 and XHTML. This means that it is still supported by most web browsers, but it is not recommended for use in new web pages. If you are writing new code, you should use the figure and figcaption elements instead. The figure element is used to group related content, and the figcaption element is used to provide a caption for the content.

    Syntax to Create Table’s Header and Caption

    The following is the syntax to create a header and caption for an HTML table:

    <table><caption>Description of table</caption><tr><th>heading 1</th><th>heading 2</th><th>heading 3</th></tr></table>

    Define a Header Row for a Table

    The <th> tag is used to represent table headings, and it is typically used within the <tr> (table row) element. Unlike the <td> (table data) tag used for regular cells, <th> is reserved for headers. In most cases, the first row of a table is designated as the header row.

    Example

    Consider a simple HTML table with headings for “Name” and “Salary”:

    <!DOCTYPE html><html lang="en"><head><title>HTML Table Header</title></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Styling Table Headings

    Styling table headings can enhance the visual appeal of a table. CSS can be applied to <th> elements to customize their appearance. In the following example, a simple CSS style is added to the <style> tag within the <head> section to modify the background color and text alignment of the table headings.

    Example

    This example demonstrates how you can style table headings with CSS:

    <!DOCTYPE html><html lang="en"><head><title>Styled HTML Table Header</title><style>
       th {
    
      background-color: #4CAF50;
      color: white;
      text-align: left;
      padding: 8px;
    } </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><td>Shabbir Hussein</td><td>7000</td></tr></table></body></html>

    Using Header Cells in Any Row

    While it’s common to use <th> in the first row of a table, you can utilize it in any row based on your requirements. This flexibility allows for the creation of complex tables with multiple header rows or headers interspersed within the table.

    Example

    In this example, we are creating table headers in the first row:

    <!DOCTYPE html><html lang="en"><head><title>Styled HTML Table Header</title><style>
    
      th {
         background-color: #4CAF50;
         color: white;
         text-align: left;
         padding: 8px;
      }
    </style></head><body><table border="1"><tr><th>Name</th><th>Salary</th></tr><tr><td>Ramesh Raman</td><td>5000</td></tr><tr><th>Additional Details</th><th>Specialization</th></tr><tr><td>Technical Lead</td><td>Web Development</td></tr></table></body></html>

    Table Header Using <thead> Element

    The <thead> tag is used to group table header cells so that a combined CSS style can be applied to header cells.

    The <thead> tag is typically placed within the <table> element and contains one or more <tr> elements, each of which, in turn, contains <th> elements representing column headers.

    Example

    In this example, we are creating a table header using the thead tag:

    <!DOCTYPE html><html lang="en"><head><title>HTML Table Header</title></head><body><table border=1><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><!-- Table body goes here --></table></body></html>

    Defining Multiple Header Rows

    You can include multiple <tr> elements within <thead> to create multiple header rows. This is useful when your table structure requires a more complex hierarchy of headers.

    Example

    In this example, we are defining two rows as the table header:

    <!DOCTYPE html><html lang="en"><head><title>HTML Table Header</title></head><body><table border=1><thead><tr><th colspan=2>Tutorialspoint</th></tr><tr><th>Role</th><th>Experience</th></tr></thead><tr><td>Technical Lead</td><td>5 Years</td></tr><tr><td>Web Developer</td><td>2 Years</td></tr></table></body></html>

    Using ‘<colgroup>’ Inside ‘<thead>’

    The <colgroup> tag can be used within <thead> to group together a set of column and apply CSS styling or attributes to entire columns.

    Example

    In this example we apply style to first two columns of table by grouping those columns in colgroup tag.

    <!DOCTYPE html><html lang="en"><head><style>
    
      .col1 {
         background-color: #f2f2f2;
      }
    </style></head><body><h1>Table with colgroup</h1><table border="1"><colgroup class="col1"><col style="width: 150px;"><col style="width: 200px;"></colgroup><col style="width: 150px;"><col style="width: 100px;"><thead><tr><th>Product ID</th><th>Product Name</th><th>Category</th><th>Price</th></tr></thead><tbody><tr><td>1</td><td>Smartphone</td><td>Electronics</td><td>$299.00</td></tr><tr><td>2</td><td>Office Chair</td><td>Furniture</td><td>$89.00</td></tr><tr><td>3</td><td>Laptop</td><td>Electronics</td><td>$999.00</td></tr></tbody></table></body></html>

    Combining with ‘<tfoot>’ and ‘<tbody>’

    The <thead> tag is often combined with <tfoot> (table footer) and <tbody> (table body) to create a comprehensive table structure.

    Example

    In the following code the structure of table separates header, body, and footer content for better organization.

    <!DOCTYPE html><html lang="en"><head><title>HTML Table</title></head><body><table border><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tbody><tr><td>value 1</td><td>value 2</td><td>value 3</td></tr></tbody><tfoot><tr><td>Total</td><td></td><td></td></tr></tfoot></table><p>
    
      Footers are generally used to enter 
      sum of values of each column.
    </p></body></html>

    Difference between <thead> and <th>

    Following are the points that highlight the differences between <thead> and <th> −

    • The <thead> tag is a structural element used to group header content, while <th> is a cell-level element defining a header cell.
    • It’s common to use <th> within <thead>, but <th> can also be used outside <thead> to define headers in regular rows.
    • Including <thead> is optional; however, using it improves the semantic structure of the table.

    Adding Caption to a HTML Table

    The caption tag will serve as a title or explanation for the table, and it shows up at the top of the table.

    Example

    In the following code we will display a caption on top of a HTML table:

    <!DOCTYPE html><html><head><title>HTML Table Caption</title></head><body><table border="1"><caption>This is the caption</caption><tr><td>row 1, column 1</td><td>row 1, column 2</td></tr><tr><td>row 2, column 1</td><td>row 2, column 2</td></tr></table></body></html>

    Table Header, Body, and Footer

    Tables can be divided into three portions: a header, a body, and a foot. The head and foot are rather similar to headers and footers in a word-processed document that remain the same for every page, while the body is the main content holder of the table.

    The three elements for separating the head, body, and foot of a table are −

    • The <thead> tag to create a separate table header.
    • The <tbody> tag to indicate the main body of the table.
    • The <tfoot> tag to create a separate table footer.

    A table may contain several <tbody> elements to indicate different pages or groups of data. But it is notable that <thead> and <tfoot> tags should appear before <tbody>

    Example

    In this example, we are creating an HTML table with the table header, body, and footer:

    <!DOCTYPE html><html><head><title>HTML Table</title></head><body><table border="1" width="100%"><thead><tr><th colspan="4">
    
               This is the head of the table
            &lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tfoot&gt;&lt;tr&gt;&lt;td colspan="4"&gt;
               This is the foot of the table
            &lt;/td&gt;&lt;/tr&gt;&lt;/tfoot&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cell 1&lt;/td&gt;&lt;td&gt;Cell 2&lt;/td&gt;&lt;td&gt;Cell 3&lt;/td&gt;&lt;td&gt;Cell 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cell 5&lt;/td&gt;&lt;td&gt;Cell 6&lt;/td&gt;&lt;td&gt;Cell 7&lt;/td&gt;&lt;td&gt;Cell 8&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Tables

    HTML tables represent data, such as text, images, etc. in a structured format with rows and columns.

    HTML tables offer a visual structure that aids in clarity and comprehension, making them a fundamental element in web development.

    HTML Tables

    Why HTML Tables are Used?

    HTML tables are used for various reasons, primarily centered around organizing and presenting data effectively. Some key purposes include −

    • Structuring Data − Tables provide a coherent structure for organizing and displaying data, making it easier for users to interpret information.
    • Comparative Presentation − When there is a need to compare different sets of data side by side like difference between two concepts, tables offer a clear and visually accessible format.
    • Tabular Data Representation − Information that naturally fits into rows and columns, such as schedules, statistics, or pricing tables, can be well-represented using HTML tables.

    Creating an HTML Table

    You can create a table in HTML by using the <table> tag along with several tags that define the structure and content inside the table. The primary tags that are used with the <table> tag are <tr><td>, and <th>.

    Creating tables in HTML involves several elements that define the structure and content. The primary tags used are <table>, <tr>, <td>, and <th>.

    • HTML <table> Tag: This tag is used to create the table that wrap the rows and columns within it.
    • HTML <tr> Tag: Stands for “table row” and is used to create a row within the table.
    • HTML <td> Tag: Represents “table data” and is used to create standard cells within a row.
    • HTML <th> Tag: Represents “table header” and is used to create header cells within a row.

    HTML Table Structure – Rows and Columns

    • Rows: Each row in an HTML table is defined using the <strong>&lt;tr></strong> tag. It contains a set of table cells (<strong>&lt;td></strong> or <strong>&lt;th></strong>), representing the individual elements within that row.
    • Columns: The actual data or header information is contained within the table cells. Cells in the same position in different rows form a column.
    • A table row is defined by the <tr> tag. To set table header, we use <th> tag. To insert data in table cell, use the <td> tag.
    • A table in HTML consists of table cells inside rows and columns of the table.
    • Table heading is defined by the <th>…</th>. Data inside the <th> are the headings of the column of a table.
    • Each table cell is defined by a <td>…</td> tag. Data inside the <td> tag are the content of the table rows and columns.
    • Each table row starts with a <tr> ….</tr> tag.
    • We use style sheet to create border for the table.

    Example

    Consider a table representing a simple list of products with their respective categories and prices. This basic table structure organizes data in a clear, tabular format, making it easy to read and understand. Here, the border is an attribute of <table> tag and it is used to put a border across all the cells. If you do not need a border, then you can use border=”0″.

    <!DOCTYPE html><html><body><table border="1"><tr><th>Product</th><th>Category</th><th>Price</th></tr><tr><td>Laptop</td><td>Electronics</td><td>$800</td></tr><tr><td>Bookshelf</td><td>Furniture</td><td>$150</td></tr><tr><td>Coffee Maker</td><td>Appliances</td><td>$50</td></tr></table></body></html>

    Styling HTML Tables

    You can also style an HTML table using CSS properties to give it a custom appearance. Either you can create classes to apply styles on a table, or you can simply write internal CSS properties to style the table.

    Example

    In the example below, we are styling the table with some CSS properties to make it stylish:

    <!DOCTYPE html><html><head><style>
       table {
    
      width: 100%;
      border-collapse: collapse;
      margin-bottom: 20px;
    } th, td {
      border: 1px solid #ddd;
      padding: 8px;
      text-align: left;
    } th {
      background-color: #f2f2f2;
    } </style></head><body><h2>HTML Table</h2><p>This table is 3*3 cells including table header.
    &lt;table&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Data 1&lt;/td&gt;&lt;td&gt;Data 2&lt;/td&gt;&lt;td&gt;Data 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Data 4&lt;/td&gt;&lt;td&gt;Data 5&lt;/td&gt;&lt;td&gt;Data 6&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Table Background Color and Image

    You can set the background color and background image of an HTML table by using the CSS and attributes of the <table> tag.

    Using Attributes

    The following are the attributes that can be used with <table> tag to set the background color and/or image:

    • bgcolor: The bgcolor attribute sets the table's background color.<table bgcolor="#f0f0f0">
    • background: The background attribute sets a background image.<table background="image.jpg">

    Using CSS Properties

    Using table tag's attributes is an old (outdated) style. It is recommended that you should use CSS to style an HTML table. The background-color and background-image properties are used to set background color and image respectively.

    table {
      background-color: #f0f0f0;
      background-image: url('image.jpg');
    }

    Example to set table's background color and image using attributes

    Here we are setting background color and image for a table using the attributes of <table> tag:

    <!DOCTYPE html><html><head><title>HTML Table Background Color</title></head><body><table border="1" bordercolor="green" bgcolor="yellow" background="/images/test.png"><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr><tr><td rowspan="2">Row 1 Cell 1</td><td>Row 1 Cell 2</td><td>Row 1 Cell 3</td></tr><tr><td>Row 2 Cell 2</td><td>Row 2 Cell 3</td></tr><tr><td colspan="3">Row 3 Cell 1</td></tr></table></body></html>

    Example to set table's background color and image using CSS

    Here we are setting background color and image for a table using the CSS properties:

    <!DOCTYPE html><html><head><title>HTML Table Background Color</title><style>
    table {
      background-color: yellow;
      background-image: url('/images/test.png');
    }
    </style></head><body><table><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr><tr><td rowspan="2">Row 1 Cell 1</td><td>Row 1 Cell 2</td><td>Row 1 Cell 3</td></tr><tr><td>Row 2 Cell 2</td><td>Row 2 Cell 3</td></tr><tr><td colspan="3">Row 3 Cell 1</td></tr></table></body></html>

    Table Width and Height

    The table's width and height can be set using either attributes or CSS properties. These values can be defined in pixels or percentages.

    Using Attributes

    The following attributes can set the width and height of a table:

    • width: It defines the width of the table.<table width="80%">
    • height: It defines the height of the table.<table height="200">

    Using CSS

    The following CSS properties can set the width and height of a table:

    • width: It defines the width of the table.table { width: 80%; }
    • height: It defines the height of the table.table { height: 400px; }

    Example to set table's width and height using attributes

    Here we are setting width (80%) and height (400px) of the table using the <table> tag's attributes:

    <!DOCTYPE html><html><head><title>HTML Table Width/Height</title></head><body><table border="1" width="80%" height="400"><tr><th>Header 1</th><th>Header 2</th></tr><tr><td>Row 1, Column 1</td><td>Row 1, Column 2</td></tr><tr><td>Row 2, Column 1</td><td>Row 2, Column 2</td></tr></table></body></html>

    Example to set table's width and height using CSS

    Here we are setting width (80%) and height (400px) to the table using the CSS properties:

    <!DOCTYPE html><html><head><title>HTML Table Width/Height</title><style>
       table{
    
       width: 80%;
       height: 400px;
    } </style></head><body><table border="1"><tr><th>Header 1</th><th>Header 2</th></tr><tr><td>Row 1, Column 1</td><td>Row 1, Column 2</td></tr><tr><td>Row 2, Column 1</td><td>Row 2, Column 2</td></tr></table></body></html>

    HTML Nested Tables

    Nested HTML tables refer to create tables inside a table. You can create tables inside a table by using the <table> tab inside any <td> tag, it creates another table in the main table's cell.

    Example

    In the following example, we are creating nested tables:

    <!DOCTYPE html><html><head><title>HTML Nested Tables</title></head><body><table border=1><tr><td> First Column of Outer Table </td><td><table border=1><tr><td> First row of Inner Table </td></tr><tr><td> Second row of Inner Table </td></tr></table></td></tr><tr><td><table border=1><tr><td> First row of Inner Table </td></tr><tr><td> Second row of Inner Table </td></tr></table></td><td> First Column of Outer Table </td></tr></table></body></html>

    Table-related Tags Reference

    The following are the table-related tags. You can click on the link to read about the specific tag and click on "Try It" to practice its example:

    TagDescriprtionExample
    <table>It is used to create HTML table.Try It
    <th>This tag defines the header of the table.Try It
    <tr>This tag defines a table row.Try It
    <td>This tag is used to store table data of each cell.Try It
    <caption>This tag specifies the caption for the table.Try It
    <colgroup>This tag describes the collection of one or more columns in a table for formattig.Try It
    <col>This tag is used to offer information about columns.Try It
    <thead>This tag is used to define table header section.Try It
    <tbody>This tag is used to define table body section.Try It
    <tfoot>This tag is used to define the table footer section.Try It