Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape

HTML Tables Tutorial: Learn How to Create Tables in HTML with Examples

    HTML Tables are one of the most useful features of HTML for displaying structured information on a webpage. Whether you want to show student details, course information, pricing, schedules, marks, or product data, HTML tables help organize information into rows and columns.

    In this guide, you will learn how to create tables in HTML, understand important HTML table tags, and explore practical examples.


    What Is an HTML Table?

    An HTML table is used to display data in a structured format using rows and columns.

    For example:

    Student NameCourseCity
    MahaveerWeb DesigningJaipur
    RahulHTML & CSSJaipur
    PriyaWeb DevelopmentJaipur

    To create a table like this, HTML provides several important tags.


    Basic HTML Table Tags

    The main tags used to create an HTML table are:

    TagPurpose
    <table>Creates the complete table
    <tr>Creates a table row
    <th>Creates a table heading
    <td>Creates a table data cell

    Let’s understand each tag.


    1. The <table> Tag

    The <table> tag is used to create the main table container.

    Example:

     
     
    <table>
    </table>
     

    All rows, headings, and data cells are placed inside the <table> tag.


    2. The <tr> Tag – Table Row

    The <tr> tag stands for Table Row.

    Each <tr> creates a new row inside the table.

    Example:

     
     
    <table>
    <tr>
    </tr>
    </table>
     

    You can create multiple rows by using multiple <tr> tags.


    3. The <th> Tag – Table Heading

    The <th> tag stands for Table Header.

    It is generally used for column headings.

    Example:

     
     
    <tr>
    <th>Name</th>
    <th>Course</th>
    <th>City</th>
    </tr>
     

    The browser usually displays table headings in bold text and centers them by default.


    4. The <td> Tag – Table Data

    The <td> tag is used to add actual data inside a table.

    Example:

     
     
    <tr>
    <td>Mahaveer</td>
    <td>Web Designing</td>
    <td>Jaipur</td>
    </tr>
     

    Creating Your First HTML Table

    Now let’s combine everything together.

     
     
    <!DOCTYPE html>
    <html>
    <head>
    <title>HTML Table Example</title>
    </head>
     
    <body>
     
    <table border=“1”>
     
    <tr>
    <th>Student Name</th>
    <th>Course</th>
    <th>City</th>
    </tr>
     
    <tr>
    <td>Mahaveer</td>
    <td>Web Designing</td>
    <td>Jaipur</td>
    </tr>
     
    <tr>
    <td>Rahul</td>
    <td>HTML & CSS</td>
    <td>Jaipur</td>
    </tr>
     
    <tr>
    <td>Priya</td>
    <td>Web Development</td>
    <td>Jaipur</td>
    </tr>
     
    </table>
     
    </body>
    </html>
     

    Output

    Student NameCourseCity
    MahaveerWeb DesigningJaipur
    RahulHTML & CSSJaipur
    PriyaWeb DevelopmentJaipur

    Understanding colspan in HTML Tables

    The colspan attribute allows a table cell to occupy multiple columns.

    For example:

     
     
    <table border=“1”>
     
    <tr>
    <th colspan=“3”>Student Information</th>
    </tr>
     
    <tr>
    <th>Name</th>
    <th>Course</th>
    <th>City</th>
    </tr>
     
    <tr>
    <td>Mahaveer</td>
    <td>Web Designing</td>
    <td>Jaipur</td>
    </tr>
     
    </table>
     

    Here:

     
     
    colspan=”3″
     

    means the heading Student Information will cover three columns.


    Understanding rowspan in HTML Tables

    The rowspan attribute allows a table cell to occupy multiple rows.

    Example:

     
     
    <table border=“1”>
     
    <tr>
    <th>Name</th>
    <th>Course</th>
    </tr>
     
    <tr>
    <td rowspan=“2”>Mahaveer</td>
    <td>HTML</td>
    </tr>
     
    <tr>
    <td>CSS</td>
    </tr>
     
    </table>
     

    In this example, the name Mahaveer occupies two rows.


    Adding Borders to HTML Tables

    A simple method for beginners is:

     
     
    <table border=“1”>
     

    However, in modern web development, CSS is preferred for styling tables.

    Example:

     
     
    <style>
     
    table {
    border-collapse: collapse;
    width: 100%;
    }
     
    th, td {
    border: 1px solid black;
    padding: 10px;
    }
     
    </style>
     

    Then create your table normally:

     
     
    <table>
     
    <tr>
    <th>Name</th>
    <th>Course</th>
    <th>City</th>
    </tr>
     
    <tr>
    <td>Mahaveer</td>
    <td>Web Designing</td>
    <td>Jaipur</td>
    </tr>
     
    </table>
     

    Complete HTML Table Example with CSS

    Here is a practical example:

     
     
    <!DOCTYPE html>
    <html>
     
    <head>
     
    <title>Student Table</title>
     
    <style>
     
    table {
    width: 100%;
    border-collapse: collapse;
    }
     
    th {
    background-color: #1e3a8a;
    color: white;
    }
     
    th, td {
    border: 1px solid #cccccc;
    padding: 12px;
    text-align: left;
    }
     
    tr:hover {
    background-color: #f2f2f2;
    }
     
    </style>
     
    </head>
     
    <body>
     
    <h1>Student Information</h1>
     
    <table>
     
    <tr>
    <th>Student Name</th>
    <th>Course</th>
    <th>City</th>
    </tr>
     
    <tr>
    <td>Mahaveer</td>
    <td>Web Designing</td>
    <td>Jaipur</td>
    </tr>
     
    <tr>
    <td>Rahul</td>
    <td>HTML & CSS</td>
    <td>Jaipur</td>
    </tr>
     
    <tr>
    <td>Priya</td>
    <td>Web Development</td>
    <td>Jaipur</td>
    </tr>
     
    </table>
     
    </body>
     
    </html>
     

    Common HTML Table Attributes

    Here are some important attributes beginners should understand:

    AttributePurpose
    colspanCombines multiple columns
    rowspanCombines multiple rows
    borderAdds a basic border
    widthSets the table width
    cellpaddingOlder method for cell spacing
    cellspacingOlder method for space between cells

    For modern websites, CSS should be used instead of older HTML styling attributes whenever possible.

    Call Now