Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

how to merge 2 cells in column in html

  1. User

    how to merge 2 cells in column in html

  2. Assistant

    In HTML, if you want to merge two or more cells in a column within a table, you use the rowspan attribute. This attribute allows a cell to span multiple rows. Here's how you can do it:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Merge Cells Example</title>
        <style>
            table {
                width: 100%;
                border-collapse: collapse;
            }
            th, td {
                border: 1px solid black;
                padding: 8px;
                text-align: left;
            }
        </style>
    </head>
    <body>
    
    <table>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
        </tr>
        <tr>
            <td rowspan="2">Merged Cell</td> <!-- This cell will span two rows -->
            <td>Content 2.1</td>
            <td>Content 3.1</td>
        </tr>
        <tr>
            <td>Content 2.2</td>
            <td>Content 3.2</td>
        </tr>
    </table>
    
    </body>
    </html>
    

    In this example:

    • The cell containing the text "Merged Cell" uses the rowspan="2" attribute, which tells the browser to make this cell span two rows in the same column.
    • This results in a table where "Merged Cell" occupies the space of the first two rows in its column, and the cells below adjust accordingly.

    You can adjust the value of rowspan to span more rows if needed. Make sure to reduce the number of rows you define in the table below the merged cell to avoid breaking the structure.