Table of Contents
Using colspan to merge cells horizontally
Using rowspan to merge cells vertically
Combining colspan and rowspan
Key Tips
Home Web Front-end HTML Tutorial How to use colspan and rowspan to merge cells in an HTML table

How to use colspan and rowspan to merge cells in an HTML table

Aug 07, 2025 pm 11:53 PM
html table Cell merge

To merge cells in HTML tables, you need to use colspan and rowspan properties. 1. Use colspan to make cells span multiple columns horizontally, such as

Contact Information to cover two columns; 2. Use rowspan to make cells span multiple rows vertically, such as Sales to make cells display across two rows; 3. You can use colspan and rowspan to achieve complex layouts in the same cell, but you need to ensure that the total number of columns per row is consistent and no cells are missing; 4. It is recommended to add borders during development to clearly view the structure, and avoid overuse of these properties to maintain maintainability and accessibility, and then encode the table structure.

How to use colspan and rowspan to merge cells in an HTML table

To merge cells in an HTML table, you can use the colspan and rowspan attributes. These allow a single table cell to span across multiple columns or rows, which is useful for creating more complex and organized table layouts.

How to use colspan and rowspan to merge cells in an HTML table

Using colspan to merge cells horizontally

The colspan attribute lets a cell stretch across multiple columns. This is helpful when you want a header or data cell to cover more than one column.

For example:

How to use colspan and rowspan to merge cells in an HTML table
 <table border="1">
  <tr>
    <th>Name</th>
    <th colspan="2">Contact Information</th>
  </tr>
  <tr>
    <td>John Doe</td>
    <td>Email: john@example.com</td>
    <td>Phone: 123-456-7890</td>
  </tr>
</table>

In this case, the "Contact Information" header spans two columns, so it covers both the Email and Phone fields. You don't need a separate cell for the third column in the first row because the colspan="2" takes up that space.

Important: When using colspan , reduce the number of additional <td> or <th> elements in that row accordingly so you don't exceed the intended table structure.

How to use colspan and rowspan to merge cells in an HTML table

Using rowspan to merge cells vertically

The rowspan attribute makes a cell span across multiple rows. This is often used for side headers or when data repeats down a column.

Example:

 <table border="1">
  <tr>
    <th>Department</th>
    <th>Employee</th>
    <th>Extension</th>
  </tr>
  <tr>
    <td rowspan="2">Sales</td>
    <td>John Doe</td>
    <td>101</td>
  </tr>
  <tr>
    <td>Jane Smith</td>
    <td>102</td>
  </tr>
</table>

Here, the "Sales" department cell spans two rows, so it appears next to both John and Jane without needing to repeat the word "Sales".

Note: In the second row, we don't include a cell for the department because it's already occurred by the spanning cell from the previous row.

Combining colspan and rowspan

You can use both attributes on the same cell if needed.

Example:

 <table border="1">
  <tr>
    <td rowspan="2" colspan="2">Combined Cell</td>
    <td>Cell 3</td>
  </tr>
  <tr>
    <td>Cell 4</td>
  </tr>
</table>

This creates a large cell in the top-left that spans two rows and two columns. The layout must be carefully managed so that the total number of columns per row still matches after accounting for all spans.

Key Tips

  • Always count your columns per row, including those taken up by colspan .
  • Make sure rowspan doesn't cause missing cells in lower rows.
  • Use border="1" during development (or CSS borders) to clearly see cell boundaries.
  • Avoid overusing colspan and rowspan —they can make tables harder to maintain and less accessible.

Basically, just plan your table structure on paper first if it's complex. Once you understand how the grid fills in, using colspan and rowspan becomes straightforward.

The above is the detailed content of How to use colspan and rowspan to merge cells in an HTML table. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Keyboard shortcut for quickly merging cells in Word Keyboard shortcut for quickly merging cells in Word Feb 18, 2024 pm 12:08 PM

Word shortcut keys for merging cells When editing tables using Microsoft Word, we often need to merge cells in order to adjust the structure and layout of the table. Word provides several ways to merge cells, including using shortcut keys. This article will introduce the shortcut keys for merging cells in Word to help you operate tables more efficiently. In Word, you can use shortcut keys to merge cells. The following are some commonly used shortcut keys for merging cells: Ctrl+Shift+

Pandas Beginner's Guide: HTML Table Data Reading Tips Pandas Beginner's Guide: HTML Table Data Reading Tips Jan 09, 2024 am 08:10 AM

Beginner's Guide: How to Read HTML Tabular Data with Pandas Introduction: Pandas is a powerful Python library for data processing and analysis. It provides flexible data structures and data analysis tools, making data processing simpler and more efficient. Pandas can not only process data in CSV, Excel and other formats, but can also directly read HTML table data. This article will introduce how to use the Pandas library to read HTML table data, and provide specific code examples to help beginners

How to use HTML tags in HTML tables? How to use HTML tags in HTML tables? Sep 08, 2023 pm 06:13 PM

We can easily add HTML tags in the table. HTML tags should be placed inside <td> tags. For example, add paragraph <p>…</p> tags or other available tags inside the <td> tag. Syntax The following is the syntax for using HTMl tags in HTML tables. <td><p>Paragraphofthecontext</p><td>Example 1 An example of using HTML tags in an HTML table is given below. <!DOCTYPEhtml><html><head&g

How to convert array to HTML table in PHP How to convert array to HTML table in PHP Jul 07, 2023 pm 09:31 PM

How to convert an array into an HTML table in PHP In web development, we often encounter the need to present data in table form. As a powerful server-side scripting language, PHP provides many convenient functions for operating arrays and generating HTML. We can use these functions to convert arrays into HTML tables. Below, we will introduce a simple method to achieve this function. First we need to have an array containing data. Here is an example array: $data=[['Nam

How to create table title in HTML? How to create table title in HTML? Aug 30, 2023 pm 07:33 PM

Create titles using tags in HTML. Tags in HTML are used to specify title cells or headers in tables. Here are the properties: Property Value Description abbrabbbreviated_text Deprecated - Specifies an abbreviated version of the content in the header cell. alignrightleftcenterjustifychar DEPRECATED - Alignment of content in header cells. axis name is deprecated - specifies the category of this th. bgcolorrgb(x,x,x)#hexcodecolorname Deprecated - Specifies the background color of the header cell. char deprecated - The character that specifies the alignment of the text. When align="char&qu

How to prevent words in HTML table from breaking into lines? How to prevent words in HTML table from breaking into lines? Sep 16, 2023 pm 10:45 PM

When you need to break a line, you can use the word-break property in CSS to change the line break. Text line breaks usually appear only in specific positions, such as after a space or hyphen. Following is the syntax for word-break:normal|break-all|keep-all|break-word|initial|inherit; Let us read this article in depth to get a better understanding of how to prevent word breaks in HTML tables. Before that, let's take a quick look at HTML tables. HTML tables Web designers can use HTML tables to organize information such as text, images, links, and other tables into rows and columns of cells. <tabl

How to set the number of rows a table cell should span in HTML? How to set the number of rows a table cell should span in HTML? Sep 01, 2023 pm 11:01 PM

Use the rowspan property to set the number of rows a table cell should span. To merge cells in HTML, use colspan and rowspan attributes. The rowspan attribute is used to specify the number of rows the cell should span, while the colspan attribute is used to specify the number of columns the cell should span. Example<!DOCTYPEhtml><html> <head> <style> &

How can I prevent my HTML table from being formatted incorrectly? How can I prevent my HTML table from being formatted incorrectly? Aug 19, 2023 pm 09:21 PM

There is a well-supported but little-known, extremely useful CSS property that applies to tables. It changes the way tables are displayed so you can have a more reliable, consistent layout. Formatting tables appropriately is beneficial as it makes the web page more user-friendly and helps users understand the information in the table more clearly. This article will teach you how to prevent table formatting "errors" in HTML. Before we dive into this article, let’s take a quick look at tables in HTML. HTML tables HTML tables are created using the &lt;table&gt; tag, where the &lt;tr&gt; tag is used to create table rows and &lt;td&

See all articles