HTML - colspan Attribute



The HTML colspan attribute is used to define how many table columns a cell should span or extend compare to other cells.

The value for this attribute must be a non-negative integer. Its default value is 1, and if we assign a value greater than 1000 to this attribute, it is considered as false value and will be set to the default value of 1.

If you assign a negative value to it, it treats it as a false value and will automatically set the default value 1 to it.

Syntax

<tag colspan = "value"></tag>

Where the value can be any non-negative integer from range 1 to 1000.

Applies On

Below listed elements allow using of the HTML colspan attribute

Element Description
<th> HTML <th> tag defines a header cell in an HTML table.
<td> HTML <td> tag defines a standard cell in an HTML table

Examples of HTML colspan Attribute

Following codes demonstatre usages of colspan attribute

Colspan attribute with th Tag

In the following example, we are using the colspan attribute to span a header cell of a table.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'colspan' attribute</title>
   <style>
      table,
      th,
      td {
         border: 1px solid black;
      }
   </style>
</head>

<body>
   <!--HTML 'colspan' attribute-->
   <p>Example of HTML 'colspan' attribute</p>
   <h2>Students Table</h2>
   <table>
      <tr>
         <th colspan="2">Name</th>
         <th>Roll No</th>
         <th>Gender</th>
      </tr>
      <tr>
         <th>First Name</th>
         <th>Last Name</th>
      </tr>
      <tr>
         <td>Revathi</td>
         <td>Satya</td>
         <td>1</td>
         <td>Female</td>
      </tr>
      <tr>
         <td>Aman</td>
         <td>Kumar</td>
         <td>2</td>
         <td>Male</td>
      </tr>
   </table>
</body>

</html>

Colspan attribute with td tag

Considering the another scenario, where we are going to use the colspan attribute with the td tag.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'colspan' attribute</title>
   <style>
      table,
      th,
      td {
         border: 1px solid black;
         width: 30%;
      }
   </style>
</head>

<body>
   <!--HTML 'colspan' attribute-->
   <p>Example of the HTML 'colspan' attribute</p>
   <h2>Grocery Bill</h2>
   <table>
      <tr>
         <th>Name</th>
         <th>Price</th>
         <th>GST</th>
      </tr>
      <tr>
         <td>Bread</td>
         <td>50</td>
         <td>5</td>
      </tr>
      <tr>
         <td>Eggs</td>
         <td>180</td>
         <td>10</td>
      </tr>
      <tr>
         <!--colspan with value 2-->
         <td colspan="2">Total price(without GST): 230</td>
         <td>Total GST: 15</td>
      </tr>
      <tr>
         <!--colspan with value 3-->
         <td colspan="3">Total paid price(including GST): 245</td>
      </tr>
   </table>
</body>

</html>

Supported Browsers

Attribute Chrome Edge Firefox Safari Opera
colspan Yes Yes Yes Yes Yes
html_attributes_reference.htm
Advertisements