Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Table Attributes (colspan, rowspan)


HTML tables allow the use of colspan and rowspan attributes to make cells span across multiple columns or rows, respectively. This is useful for creating complex table layouts and better organizing table data.

The colspan Attribute

The colspan attribute is used to make a cell span across multiple columns. By specifying a numeric value for colspan, you can define how many columns a particular cell should cover.

Example of Using colspan:

    <table>
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>City</th>
        </tr>
        <tr>
            <td colspan="2">John Doe</td>
            <td>New York</td>
        </tr>
        <tr>
            <td>Alice</td>
            <td>30</td>
            <td>Los Angeles</td>
        </tr>
    </table>
        

In this example, the cell containing "John Doe" spans across two columns due to colspan="2". This makes "John Doe" cover both the "Name" and "Age" columns in that row.

The rowspan Attribute

The rowspan attribute is used to make a cell span across multiple rows. By specifying a numeric value for rowspan, you can define how many rows a particular cell should cover.

Example of Using rowspan:

    <table>
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Stock</th>
        </tr>
        <tr>
            <td rowspan="2">Apples</td>
            <td>$1.50</td>
            <td>In Stock</td>
        </tr>
        <tr>
            <td>$1.25</td>
            <td>Out of Stock</td>
        </tr>
    </table>
        

Here, the "Apples" cell spans across two rows, indicated by rowspan="2". This makes "Apples" occupy the first cell in both rows.

Combining colspan and rowspan

You can use both colspan and rowspan in a single table to create more complex layouts. Here’s an example combining both attributes.

Example of Using Both colspan and rowspan:

    <table>
        <tr>
            <th>Product</th>
            <th colspan="2">Details</th>
        </tr>
        <tr>
            <td rowspan="2">Oranges</td>
            <td>Price: $1.30</td>
            <td>In Stock</td>
        </tr>
        <tr>
            <td colspan="2">Fresh and juicy oranges</td>
        </tr>
    </table>
        

In this example:

Conclusion

The colspan and rowspan attributes are powerful tools in HTML tables that allow you to create complex layouts by merging cells across columns and rows. Using these attributes can improve the organization and readability of table data, especially when you need to show grouped or related data within a single table.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java