What’s Colspan?

Print anything with Printful



The colspan attribute in HTML allows a table cell to span multiple columns. It is useful for displaying information across one row. Tables are useful for organizing information on a web page, and the underlying structure can be used without the reader being aware of it. The colspan attribute can prevent a web page from looking cluttered.

The colspan attribute is an HTML feature that allows a table cell in a web page to span multiple columns. HTML is one of the main ways web pages are created. One way to bring structure into an HTML web page is the concept of a table. A table has multiple rows and columns. Information in individual table cells appears arranged vertically and horizontally.

Often, when viewing an HTML table, you want some information to be applied or spread across multiple columns. When that information is limited to one row, the colspan attribute comes in handy. Thus colspan=N, where N is a number such as 2, 3, etc., indicates that the cell spans or spans that number of columns.

Consider an example where sales data is shown as a table with three column headings: the sales region, the person who runs the region, and the sales amount. When the total is displayed, only the total sales amount is of interest. The person’s cell will be blank in that row. Rather than showing an empty cell, it’s nice to draw attention to the total instead. The following code achieves this.

In this example, a table is created and three headers, Region, Sales Manager, and Sales are specified, followed by three rows of data. Each cell of the table is indicated by the td attribute, for “table data”. In the fourth row, the phrase Total Sales is given along with the total amount. Notice that the phrase “Total Sales” is emphasized by its cell spanning two columns: the Region column and the Head column; this is achieved by specifying colspan=2. This cell spans two columns, so this row has only two td attributes and not three like in the other rows.

You can copy this code into a text file and view it in a browser to see the effect. A web page may not require displaying data within table cells as often as in the previous example. However, the concept of a table is very useful for organizing information on a web page.
A web page often consists of a header at the top, such as the company name and logo, one or more menus at the top or sides, main information in the center, and summary information at the bottom. To achieve this, an underlying table structure can be used without the reader being aware of it. Quite often some information is spread across multiple columns using the colspan attribute, preventing the web page from looking clunky.
For example, you might want a personal page with three columns: Family, Career, and Community. It would be nice to break up monotonous columns with a picture, somewhere at the bottom of the page, where it spreads across all columns. The following code achieves this with a span value of 3.




Protect your devices with Threat Protection by NordVPN


Skip to content