HTML Tables

HTML tables are used to organize and display data in rows and columns. Tables help present structured information such as student records, employee details, schedules, and product information. A table is created using the <table> tag and contains rows <tr>, headings <th>, and data cells <td>.


Table Syntax


<table>

    <tr>

        <th>Name</th>

        <th>Course</th>

        <th>Year</th>

    </tr>



    <tr>

        <td>John</td>

        <td>HTML</td>

        <td>2026</td>

    </tr>


</table>


Example


<table border="1">


    <tr>

        <th>Name</th>

        <th>Course</th>

        <th>Year</th>

    </tr>



    <tr>

        <td>John</td>

        <td>HTML</td>

        <td>2026</td>

    </tr>



    <tr>

        <td>David</td>

        <td>CSS</td>

        <td>2026</td>

    </tr>


</table>

Output

Name Course Year
John HTML 2026
David CSS 2026

Table Elements

HTML tables are created using different table elements:

  • <table> - Creates a table.
  • <tr> - Creates a table row.
  • <th> - Creates a table heading.
  • <td> - Creates a table data cell.
  • <caption> - Adds a title to the table.
  • <thead> - Defines table header section.
  • <tbody> - Defines table body section.
  • <tfoot> - Defines table footer section.

Best Practices

  • Use tables only for displaying tabular data.
  • Always use <th> for headings.
  • Keep table information organized and readable.
  • Use <caption> to describe tables.
  • Avoid using tables for webpage layout.
  • Use CSS for styling tables instead of HTML attributes.