How to define the number of columns a cell should span using HTML5 ?

This article will show you how a column can be made to span over multiple cells. This is done using the colspan attribute when using the <td> tag. This allows the single table cell to span the width of more than one cell or column. It provides similar functionality to the “merge cell” functions in spreadsheet programs like Excel.

Syntax:

<td colspan = "value"<table content...</td>

Example:

html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        body {
            text-align: center;
        }
  
        h1 {
            color: green;
        }
  
        table,
        tbody,
        td {
            border: 1px solid black;
            border-collapse: collapse;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
  
    <h2>
        How to define the number of
        columns a cell should span
        using HTML5?
    </h2>
  
    <table>
  
        <!-- The <tr> tag starts here -->
        <tr>
            <th>Name</th>
            <th>User ID</th>
        </tr>
        <tr>
            <td>Anmol</td>
            <td>345</td>
        </tr>
        <tr>
            <td>Priya</td>
            <td>567</td>
        </tr>
  
        <!-- The last row -->
        <tr>
            <!-- This td will span two 
                columns, that is a single 
                column will take up the 
                space of 2 -->
            <td colspan="2">
                Sumit: 6574
            </td>
        </tr>
    </table>
</body>
  
</html>

chevron_right


Output:

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.