Rowspans & Colspans in CSS Tables

Share this article

“…we’ve implemented every property in CSS 2.1 and are closing in on our goal of complete support for the CSS 2.1 specification by the time we release.”
If you were to guess who recently made that statement you’d be forgiven for thinking it came from the Opera, Safari, or Firefox team; they have always seemed to be the most standards-conscious browser vendors. In fact, this quote comes from Doug Stamper of Microsoft, regarding Internet Explorer 8. It seems the very thing web designers have been asking for—mature support for CSS2.1 across all major browsers—is actually about to happen. Back in Tech Times #185, I wrote about what this would mean to web designers in Table-Based Layout Is The Next Big Thing. In short, I said that CSS tables would become the best tool for CSS page layout. There were mixed reactions to that article, particularly on the point of row and column spans. HTML tables let you create cells that span multiple rows or columns, but CSS tables don’t provide that same freedom. Well, in my research for an as yet unannounced, potentially controversial book on CSS, I’ve figured out how to simulate row and column spans in CSS tables. Rather than make you wait for the book, I thought I’d show you this useful technique right away!

Nothing Up My Sleeve…

If you’ve had experience building layouts using HTML tables, you’ll be familiar with the use of the colspan and rowspan attributes of the td element. These attributes offer complex possibilities to a simple table, enabling cells to span columns and rows. CSS tables lack any concept of row or column spanning, making it trickier to use one single layout structure than what might have been possible when using tables. However, similar layouts can be achieved by using nested CSS tables. Of course, nested tables are not a perfect solution. When you want to match the dimensions of cells across nested tables, for example, things can get messy in a hurry, and the extra <div> tags really start to add up. As it turns out, it’s also possible to simulate row and column spanning using absolute positioning of table cells, in many cases. In this example, we’ll make the second cell of the first row of a table span both rows of the table (as if it had a rowspan of 2). First, let’s take a look at the HTML code:
<div class="tablewrapper">
  <div class="table">
    <div class="row">
      <div class="cell">
        Top left
      </div>
      <div class="rowspanned cell">
        Center
      </div>
      <div class="cell">
        Top right
      </div>
    </div>
    <div class="row">
      <div class="cell">
        Bottom left
      </div>
      <div class="empty cell"></div>
      <div class="cell">
        Bottom right
      </div>
    </div>
  </div>
</div>
You’ll notice that we’ve wrapped our table div in an extra div with a class
of "tablewrapper". This extra div is needed to provide a CSS positioning context—which we create by giving it relative positioning:
.tablewrapper {
  position: relative;
}
According to the CSS spec, we should be able to simply apply relative positioning to the table div, but current browsers don’t seem to support this.

Sleight of Hand and Absolute Positioning

Now, we can use absolute positioning to control the size and position of the div with class "rowspanned cell":
.cell.rowspanned {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100px;
}
With the top and bottom properties both set to zero, the cell will stretch to fill the full height of the table, simulating a row span. Depending on the needs of your layout, you could use different values for top and bottom, or even set the cell’s height directly to achieve other row-spanning layouts. You also need to specify the width of the cell. Usually, the easiest way to do this is just to set its width
property, but depending what you know of the dimensions of surrounding table cells, you could also do this by setting left and right. Since the positioned cell doesn’t actually span multiple rows of the table, the table must still contain a corresponding cell in each of the other rows. These cells are simply empty placeholders, though; note the div with class "empty cell" in the HTML code above. The function of this cell is to hold open the space that will be occupied by the “spanned” cell, so we must ensure its width matches the width we specified for the "rowspanned cell":
.cell.empty {
  width: 100px;
}
And that’s all there is to it! To complete the style sheet for this example, we need only set the appropriate display property values, and add some borders so we can see what’s going on:
.tablewrapper {
  position: relative;
}
.table {
  display: table;
}
.row {
  display: table-row;
}
.cell {
  border: 1px solid red;
  display: table-cell;
}
.cell.empty
{
  border: none;
  width: 100px;
}
.cell.rowspanned {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100px;
}
In essence, by using absolute positioning we are telling the browser, “Let me handle the layout of this table cell—you take care of the rest.” Here’s what the results look like: Try it for yourself. This example works in all major browsers except for Internet Explorer 7, and also works in the current IE8 Beta 2 release. What do you think? Can you see yourself switching to CSS tables for layout as your Internet Explorer users make the move to IE8?

Frequently Asked Questions (FAQs) about Rowspans and Colspans in CSS Tables

What is the difference between rowspan and colspan in CSS tables?

In CSS tables, rowspan and colspan are attributes that allow you to control how many rows or columns a cell should span. The rowspan attribute allows a cell to span across multiple rows vertically, while the colspan attribute allows a cell to span across multiple columns horizontally. This is particularly useful when you want to merge cells for aesthetic purposes or to group related data together.

How do I use rowspan and colspan in CSS tables?

To use rowspan and colspan in CSS tables, you need to add these attributes to the td or th elements in your table. For example, if you want a cell to span across two rows, you would write

. Similarly, if you want a cell to span across three columns, you would write .

Can I use both rowspan and colspan in the same cell?

Yes, you can use both rowspan and colspan in the same cell. This allows you to create a cell that spans across multiple rows and columns. For example,

would create a cell that spans across two rows and three columns.

Why are my rowspan and colspan not working?

There could be several reasons why your rowspan and colspan are not working. One common reason is that you have not allocated enough cells in your table. Remember, if you want a cell to span across two rows, there must be an empty cell in the row below for it to expand into. Similarly, if you want a cell to span across two columns, there must be an empty cell in the column to the right for it to expand into.

Can I use rowspan and colspan with other CSS properties?

Yes, you can use rowspan and colspan with other CSS properties. For example, you can use them with the border, padding, and background-color properties to style your table cells.

How do I control the width and height of cells using rowspan and colspan?

The rowspan and colspan attributes do not directly control the width and height of cells. Instead, they control how many rows or columns a cell should span. The actual width and height of the cell will depend on the content of the cell and the width and height of the table.

Can I use rowspan and colspan in responsive design?

Yes, you can use rowspan and colspan in responsive design. However, keep in mind that tables are not always the best choice for responsive design because they can become too small to read on smaller screens. In such cases, you might want to consider using CSS Flexbox or Grid instead.

Are there any limitations to using rowspan and colspan?

The main limitation of using rowspan and colspan is that they can make your table structure more complex, which can make it harder to maintain and update your table. Additionally, if you use them excessively, it can make your table difficult to read and understand.

Can I use rowspan and colspan in nested tables?

Yes, you can use rowspan and colspan in nested tables. However, keep in mind that the rowspan and colspan attributes will only affect the table in which they are used. They will not affect any parent or child tables.

Are there any alternatives to using rowspan and colspan?

Yes, there are alternatives to using rowspan and colspan. For example, you can use CSS Flexbox or Grid to create complex layouts without the need for rowspan and colspan. However, these techniques have their own learning curve and may not be suitable for all situations.

Kevin YankKevin Yank
View Author

Kevin Yank is an accomplished web developer, speaker, trainer and author of Build Your Own Database Driven Website Using PHP & MySQL and Co-Author of Simply JavaScript and Everything You Know About CSS is Wrong! Kevin loves to share his wealth of knowledge and it didn't stop at books, he's also the course instructor to 3 online courses in web development. Currently Kevin is the Director of Front End Engineering at Culture Amp.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week