Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
HTML Table
3.
Colspan Attribute In HTML
3.1.
Syntax
4.
Example of HTML Colspan Attribute On <td>
4.1.
Code Implementation
4.1.1.
Output
5.
Example of HTML Colspan Attribute On <th>
5.1.
Code Implementation
5.1.1.
Output
6.
Customize the Example using this Attribute along with CSS
6.1.
Code Implementation
6.1.1.
Output
7.
Browser Supports
8.
Use Case of HTML Colspan Attribute
8.1.
Code Implementation
8.1.1.
Output
8.2.
Code Implementation
8.2.1.
Output
8.3.
Code Implementation
8.3.1.
Output
9.
Pros of using HTML Colspan Attribute
10.
Cons of using HTML Colspan Attribute
11.
Frequently Asked Questions
11.1.
Can the HTML colspan attribute be used to merge cells vertically in an HTML table?
11.2.
Does the colspan attribute have any impact on the SEO of a webpage?
11.3.
Can the colspan attribute be used to create responsive tables that adjust to different screen sizes?
11.4.
Can the colspan attribute be used with other attributes in HTML tables, such as rowspan and scope?
11.5.
What happens if you specify a value for colspan attribute but is greater than the number of columns in the table?
12.
Conclusion
Last Updated: Mar 27, 2024

HTML colspan Attribute

Author Vidhi Sareen
0 upvote

Introduction

A website is the initial point of contact between a business and a client in today's digital world. A well-designed website can not only present a business idea but also can attract more visitors to their business. Where a poorly designed one might deflect them. 

There are different features of HTML. One of the important aspects of website design is how well the data is presented and organized using HTML tables. The colspan element in HTML tables is a handy tool for merging cells and generating more complicated tables in an easy way.

HTML Colspan Attribute

So in this article we are going to discuss Syntax, Usage, Examples and common points of mistake while using HTML colspan Attribute. 

Before going toward the attribute part, let's understand what an HTML table is.

HTML Table

HTML table is a way to display data in rows and columns format. It is used in organizing information in a table format. It makes it easy to read and understand and even to compare. HTML tables consist of cells where each cell consists of text, images or other HTML elements. To make it more interactive you can use CSS to adjust style or size. HTML tables can represent any type of data through a table. There are many tags used in HTML for various purposes.

 

The basic tags that are used along with the <table> in HTML are:

  • <table> used to define a table in HTML.
     
  • <tr> used to define a new row in the table.
     
  • <td> used to define the table data.
     
  • <th> used to define the header of the table.
     

To learn more about HTML tables do check the link Tables in HTML.

Colspan Attribute In HTML

The colspan Attribute in HTML is an extremely useful tool for merging cells and generating more complicated table structures very quickly. It is used to specify how many columns a cell should stretch horizontally. By default, each cell only takes up one column in the table.

The colspan property is commonly used within an HTML table's <td> or <th> tags. It accepts a positive integer value that determines how many columns a cell should span. For example, If a cell should span two columns, the colspan property should be set to "2".

Syntax

<td colspan=”Number of columns to span”>Cell content or value</td>


 Or

<th colspan=Number of columns to span”>Cell content or value</td>

Example of HTML Colspan Attribute On <td>

Let's consider an example which has multiple values for colspan Attribute.

Code Implementation

<!DOCTYPE html>
<html>
<head>
<title>HTML Colspan Attribute Example</title>
	<style>
		table, th, td {
		border: 1px solid black;
		border-collapse: collapse;
		padding: 6px;
		text-align:center;
		}
	</style>
</head>
<body>
<center>
<h1 style="color: orange;">REPORT CARD</h1>
<table>
   <tr>
		<th>Student</th>
		<th>English</th>
		<th>Maths</th>
		<th>Science</th>
		<th>Hindi</th>
	</tr>
	<tr>
		<td>Ninja_1</td>
		<td>95</td>
		<td>82</td>
		<td>56</td>
		<td>79</td>
	</tr>
	<tr>
		<td>Ninja_2</td>
		<td>85</td>
		<td>98</td>
		<td>64</td>
		<td>67</td>
	</tr>
	<tr>
		<td>Ninja_3</td>
		<td>74</td>
		<!-- Merge two columns of subject -->
		<td colspan="2">88</td>
		<td>45</td>
	</tr>
	<tr>
		<td>Ninja_4</td>
		<!-- Merge three columns of subject -->
		<td colspan="3">55</td>
		<td>71</td>
	</tr>
</table>
</center>
</body>
</html>

 

Output

Output

In this example, the report card of the four students with four different subjects. The first row consists of subjects and the other row consists of the marks of the students in each subject.

As you can see, the marks for 'Ninja_3' for subjects 'Maths' and 'Science' are combined into one cell using the 'colspan=" 2," which merges two columns. The same is the case with 'Ninja_4' for subjects' English,' 'Maths,' and 'Science' uses 'colspan=" 3," which merges three columns.

Therefore, using the 'colspan' attribute, we can merge different columns easily.

Example of HTML Colspan Attribute On <th>

Here is another example where very merge the header rows of the HTML table.

Code Implementation

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
   border: 2px solid Blue;
}
</style>
</head>
<body>
<h2>Coding Ninja</h2>
<table>
   <tr>
      <th colspan="2">Courses</th>
   </tr>
   <tr>
      <td>Basics of C++ with Data Structures and Algorithms</td>
      <td>Fresher Level</td>
   </tr>
   <tr>
      <td>Data Science & Machine Learning Course</td>
      <td>Intermediate Level</td>
   </tr>
   <tr>
      <td>Basics of Python with Data Structures and Algorithms</td>
      <td>Fresher Level</td>
   </tr>
   <tr>
      <td>Basics of Java with Data Structures and Algorithms</td>
      <td>Fresher Level</td>
</table>
</body>
</html>

 

Output

Output


In this example as you can see, the header row which is merged using the colspan attribute.

Customize the Example using this Attribute along with CSS

Let's create a customised example for a better understanding of this colspan attribute using CSS

Code Implementation

<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
table {
	border-collapse: collapse;
	width: 80%;
	margin: auto;
	font-family: Arial, sans-serif;
	font-size: 14px;
	text-align: center;
}
table th, table td {
	border: 1px solid #ddd;
	padding: 10px;
}
table th {
	background-color: #FBE7A1;
	color: #333;
	font-weight: bold;
}
table td {
	background-color: #D2B48C;
	color: #333;
}
table tr:last-child td {
	background-color: #FFA500;
	font-weight: bold;
}
td[colspan="2"] {
	background-color: #FFA62F;
	font-weight: bold;
}
</style>
</head>
<body>
<h1 style="text-align:center;">CODING NINJAS</h1>
<table>
	<tr>
		<th colspan="2">Coding Ninjas Courses</th>
	</tr>
	<tr>
		<td>Basics of C++ with Data Structures and Algorithms</td>
		<td>Fresher Level</td>
	</tr>
	<tr>
		<td>Data Science & Machine Learning Course</td>
		<td>Intermediate Level</td>
	</tr>
	<tr>
		<td>Basics of Python with Data Structures and Algorithms</td>
		<td>Fresher Level</td>
	</tr>
	<tr>
		<td>Basics of Java with Data Structures and Algorithms</td>
		<td>Fresher Level</td>
	</tr>
	<tr>
		<td colspan="2">For more information, visit Coding Ninjas website</td>
	</tr>
</table>
</body>
</html>

 

Output

Output

As you can see that a table has been created using a CSS font family and the size to make the text clear and more understandable. We have added some background color to make it more attractive.

Browser Supports

There are the number of browsers which support the Colspan attribute are:

  • Google Chrome
  • Mozilla Firefox
  • Microsoft Edge
  • Safari
  • Opera

Use Case of HTML Colspan Attribute

There are many use cases of HTML colspan attributes like: 

1) They are used in combining two or more cells into a single cell. This is very useful in cases where you have a table with a header that stretches multiple columns. To understand this better, let a look at the example below.

Code Implementation

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
	border: 1px solid black;
	border-collapse: collapse;
	padding: 10px;
	text-align: center;
}
th {
	background-color: #FFFF00;
}
td {
	background-color: #FDBD01;
}
</style>
</head>
<body>
<table>
	<tr>
		<th colspan="2">H1</th>
		<th>H2</th>
		<th>H3</th>
	</tr>
	<tr>
		<td>Row 1, Cell 1</td>
		<td>Row 1, Cell 2</td>
		<td>Row 1, Cell 3</td>
		<td>Row 1, Cell 4</td>
	</tr>
	<tr>
		<td colspan="2">Row 2, Cell 1 and 2</td>
		<td>Row 2, Cell 3</td>
		<td>Row 2, Cell 4</td>
	</tr>
</table>
</body>
</html>

 

Output

Output

2) They are used when you want to create a complex table layout by merging different cells in different ways.To understand this better let's take a look at the example below.

Code Implementation

<!DOCTYPE html>
<html>
<head>
<title>HTML colspan Attribute Example</title>
<style>
table {
	border-collapse: collapse;
	width: 100%;
	margin-bottom: 20px;
}
table, th, td {
	border: 1px solid black;
}
th, td {
	padding: 10px;
	text-align: left;
}
th {
	background-color: #f2f2f2;
}
.yellow {
	background-color: yellow;
}
.green {
	background-color: orange;
}
</style>
</head>
<body>
<table>
	<tr>
		<td rowspan="2">Ninja_1</td>
		<td>Ninja_2</td>
		<td rowspan="2">Ninja_3</td>
		<td class="yellow">Ninja_4</td>
		<td>Ninja_5</td>
	</tr>
	<tr>
		<td>Ninja_6</td>
		<td class="green">Ninja_7</td>
		<td>Ninja_8</td>
	</tr>
</table>
</body>
</html>

 

Output

Output

 

3) You can use this to create a table with irregular columns where you want some cells' widths different from other cells.

Code Implementation

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
	border: 1px solid black;
	border-collapse: collapse;
	padding: 10px;
	text-align: center;
}
th {
	font-size: 16px;
	font-weight: bold;
}
td {
	font-size: 14px;
}
</style>
</head>
<body>
<table>
	<tr>
		<th colspan="2">Name</th>
		<th>Age</th>
		<th colspan="2">Course</th>
	</tr>
	<tr>
		<td>Ninja</td>
		<td>1</td>
		<td>20</td>
		<td>C++</td>
		<td>Python</td>
	</tr>
	<tr>
		<td>Ninja</td>
		<td>2</td>
		<td>31</td>
		<td colspan="2">Java</td>
	</tr>
	<tr>
		<td>Ninja</td>
		<td>3</td>
		<td>29</td>
		<td>JavaScript</td>
		<td>R</td>
	</tr>
</table>
</body>
</html>

 

Output

Output

Pros of using HTML Colspan Attribute

Now we are going to discuss some pros of HTML Colspan Attribute which are: 

  • It creates a simplified version of a complex table.
     
  • It is used to create a flexible layout of a table. It is very beneficial when you want to create a table with irregular columns.
     
  • It creates a table that is more visually appealing.

Cons of using HTML Colspan Attribute

Now we are going to discuss some cons of HTML Colspan Attribute which are: 

  • It creates problems while maintaining. It is especially true when you further want to create more complex table layouts.
     
  • It is easier to change the data of a cell by affecting other columns.
     
  • It also creates accessibility issues if you want to fetch data.

Check this out,  indexOf in JavaScript

Frequently Asked Questions

Can the HTML colspan attribute be used to merge cells vertically in an HTML table?

No, we can’t use ‘colspan attribute to merge tables cells vertically but you can use it horizontally. If you want to combine cells vertically then you can use the ‘rowspan’ attribute.

Does the colspan attribute have any impact on the SEO of a webpage?

The colspan attribute does not have any direct impact on the SEO (Search Engine Optimization) of a webpage. This attribute is used purely for visual layout purposes in HTML tables, and it does not affect the content or the structure of the webpage.

Can the colspan attribute be used to create responsive tables that adjust to different screen sizes?

No, the colspan attribute cannot be used to create responsive tables that adjust to different screen sizes as it only affects the layout of the table cells, not the overall table structure. Responsive design is achieved through the use of CSS media queries and other techniques.

Can the colspan attribute be used with other attributes in HTML tables, such as rowspan and scope?

Yes, the colspan attribute can be used in conjunction with other HTML table attributes such as rowspan and scope to create complex table structures and improve accessibility. These attributes allow for greater control over the layout and organization of table cells.

What happens if you specify a value for colspan attribute but is greater than the number of columns in the table?

If you have mentioned the ‘colspan’ attribute value greater than the number of columns of the table then in that case extra cells will be automatically generated, resulting in a disruptive table.

Conclusion

The HTML Colspan Attribute is a powerful and useful tool for merging cells in the HTML table.The colspan attribute is utilized to determine the number of columns that a cell should span horizontally. By default, a cell occupies only one column in a table. 

However it's important to use this attribute carefully so that it does not create any negative impact on the layout or on the content. The use of colspan attribute can cause challenges while maintaining the table, particularly if you aim to design more intricate table layouts. Additionally, modifying the content of a cell can impact other columns, making it difficult to manage data. Furthermore, colspan can lead to accessibility problems when attempting to retrieve data.

To learn more about such topic, do check the link below

You can find more informative articles or blogs on our platform. You can also practice more coding problems and prepare for interview questions from well-known companies on your platform, Coding Ninjas Studio.

Live masterclass