The Role of Table Layouts in Responsive Web Design

The Role of Table Layouts in Responsive Web Design

Tutorial Details
  • Difficulty: Intermediate
  • Topic: Responsive Web Design
  • Estimated completion time: 10 mins

Fluid layouts are the perfect way to house our content at any viewport size. Text can flow as containers grow and shrink, images and even video and iframes can flex and squish too. But what if we have content which shouldn’t alter dimensions? What about ad blocks, for example, which are often designed with very specific sizing in mind? Oddly enough, this is where table layouts come to the rescue..

I was reminded of this recently when reading Tim Kadlec’s Implementing Responsive Design. It came just at the right time too; I was in the middle of a client project which needed just this approach (and I was busy butchering it).


Ads on the Web

Many websites depend on advertising revenue. However much you may dislike being barraged with web marketing imagery, without it you wouldn’t have access to much of the free content you’ve become used to. Tuts+, for example, relies in part on advertising revenue to maintain its ever-growing team of writers and staff.

The Interactive Advertising Bureau (iab) is responsible for a huge amount of digital advertising and works hard to define standards and guidelines for the whole industry. Take a look at their Display Advertising Guidelines for a reference of the universal ad block sizes they recommend:


Medium Rectangle 300x250px

Rectangle 180x150px

Wide Skyscraper 160x600px

Leaderboard 728x90px

Four fixed dimension images. And it’s important that they remain at those dimensions too; logos, straplines and disclaimers could all become unrecognizable or illegible if squished down in a fluid layout. Also, companies who pay for these ads do so based on the blocks’ size and position. An advert of 300x250px may not be as prominent or effective when displayed at a smaller size, thereby shortchanging the company in question.

Ads are designed for display at a fixed size, so until we have a better solution for responsive advertising, it’s best to respect that.


Fluid Layout

Let’s quickly build ourselves a generic fluid layout to demonstrate our problem. A <section>, containing an <article> and an <aside>. All we need:

<section>
	<aside>
		<!--here's our ad block-->
	</aside>
	<article>
		<!--here's a load of content-->
	</article>
</section>

Then we can style it like so:

section {
	width: 90%;
	margin: 0 auto;
}
aside {
	width: 30%;
	float: left;
}
article {
	width: 65%;
	float: right;
}
img {
	max-width: 100%;
}

With some added color, this is the effect you’ll achieve:

With our img {max-width: 100%} rule we’ve ensured that our images are fluid and never break out from their parent container. Sadly, our fluid aside is narrower here than the image’s 300px width, just what we don’t want to happen.


Hybrid Layout

Let’s fix the width of the aside, leaving the article to do the flexing. As a combination of fixed and fluid, we refer to this layout type as hybrid.

section {
	width: 90%;
	margin: 0 auto;
}
aside {
	width: 300px;
	float: left;
}
article {
	width: 65%;
	float: right;
}
img {
	max-width: 100%;
}

This works on wider screens, but as soon as the container becomes too small, ie: when 300px is more than 35% of the container’s width, the article is pushed underneath:

We can solve this in our case by switching the markup around; placing the aside in the article:

<section>
	<article>
		<aside>
			<!--here's our ad block-->
		</aside>
		<!--here's a load of content-->
	</article>
</section>

Then removing the width and the float from the article, finally giving the aside a margin-right to act as the gutter:

section {
	width: 90%;
	margin: 0 auto;
}
aside {
	width: 300px;
	float: left;
	margin-right: 5%;
}
article {
}
img {
	max-width: 100%;
}

This is what we get:

Which is perfect! The article content is fluid, whereas the ad block and its parent aside are fixed width! You can even shift the aside over to the right, by swapping the float to float: right and placing the gutter (its margin) on the other side too.

Problems arise, however, when the article content begins to take up more vertical space than the aside. This is the result:

There are hacks to avoid this; we could give the aside a massive bottom padding, then an equally massive negative bottom margin. Then with an overflow: hidden we artificially lengthen the aside, hiding whatever sticks out.

Or we could use positioning and fix the aside in the top right of the article, giving the article some padding to make room for it.

Neither of these is ideal, each presenting their own unique issues.


Enter the Table Layout

What?! I hear you say…

That’s right. Table layouts (God rest their souls) were actually pretty good at arranging hybrid layouts, so let’s take a leaf out of their book. Using CSS we can display our elements as though they are components of a table. This is absolutely fine too; we’re not using actual tables in our markup (that would be bad as we’re not working with tabular data). We’re simply going to lean on tables for display purposes, without changing the semantic meaning of our document.

Going back to our original layout (where the aside and the article were side by side) we can alter the CSS to suggest table-like building blocks. Our containing section becomes the table, the article and aside become the table cells within it:

section {
	display: table;
	width: 90%;
	margin: 0 auto;
}
aside {
	display: table-cell;
	width: 300px;
}
article {
	display: table-cell;
}
img {
	max-width: 100%;
}

Which gives us:

Hmm, not quite right. We need to alter the vertical alignment of our cells, because the image and text here are both sitting on the same baseline.

aside {
	display: table-cell;
	vertical-align: top;
	width: 300px;
}
article {
	display: table-cell;
	vertical-align: top;
}

Now, whenever either the article or the aside grow vertically, the other grows with it. The arrangement does depend on the DOM though; whichever table cell element appears first in the document also appears first (to the left) of our table layout.

And of course, we can wrap this all up in a media query too, so that this layout only kicks in on larger screens. On smaller screens the aside will neatly sit on top of the article. Take a look at the demo to see how this behaves.


Support

Using CSS table display is supported in all modern browsers, but was only adopted by Internet Explorer with the release of IE8.

For earlier browsers, therefore, you’d be wise to conditionally include a fallback stylesheet, which gives you an alternative layout solution:

<!--[if lt IE 8]>
<link rel="stylesheet" href="ie.css" media="all">
<![endif]-->

(or you could just ignore IE7 and below…)

As Tim also mentions in his book, Windows 7 phones will also load in the ie.css styles we’ve just set. In order to prevent that from happening, you’ll need to alter the conditions:

<!--[if lt IE 8 & (!IEMobile)]>
<link rel="stylesheet" href="ie.css" media="all">
<![endif]-->

Conclusion

So there we have it – who would have thought table layouts could ever help you out in this age of responsive web design?! Admittedly, this isn’t the perfect solution you may have been wishing for, but until CSS Grid Layout and Flexbox gain a bit more traction, this is a great alternative.


Further Resources

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Thomas

    You could just add a margin-left: 300px to the article to solve this specific problem :)

    • ianyates

      Then position:absolute the aside? That throws up a few other issues, but it’s certainly an option :)

  • Brannon

    Another option you can consider is using CSS calc():

    HTML:

    —————————————-

    CSS:


    section {
    width: 90%;
    margin: 0 auto;
    }

    aside {
    width: 300px;
    float: left;
    }

    article {
    width: calc(100% - 300px);
    float: right;
    }

    img {
    max-width: 100%;
    }

    • ianyates

      Nice idea :) Support isn’t quite as good, but it’s a pretty elegant way to do things.

      • Brannon

        Yeah, support is pretty iffy right now. Mostly it’s an issue with IE < 9. If you use Paul Irish's conditional IE classes, you can get around that. I realize that that's browser detection instead of feature detection, which has potential holes.

    • Quicksilvertope

      well, is it going to work in all browsers especially the ever villain and freindly IE?

  • schadeck

    A follow up to @Thomas comment about using negative margins. I ran into needing a static column for a responsive site, so I came up with this solution: http://jsbin.com/uwolov/1

    Although I will have to try using the display: table; solution sometime.

    • ianyates

      Great example, thanks for that.

  • Pingback: The Role of Table Layouts in Responsive Web Design … | inLine Media RSS Blog

  • utuxia

    I find myself using table layouts a lot more for this sort of thing…specifically a fluid cell that expands and contracts. but the one caveat is that it does not lend itself to positioned children very easily. Things you would normally expect to work with a block item may not work to well inside a table-cell.

    • ianyates

      That’s right – positioning and table cells don’t marry well :( In that case you’d need to have an additional container element around whatever it is you need to position. Not ideal..

  • Pingback: Best Responsive Web Design Resources for Designers | Website Designing | Tech Design Blog

  • http://twitter.com/lchski Lucas Cherkewski

    Interestingly, when I resize the demo to a “phone width,” the aside disappears completely, as opposed to being positioned on top of the article content. Nonetheless, thanks for the great article, Ian!

  • Shiva

    Right,Good to see these useful info here..Thanks a lot for sharing them with us….

  • longbeachwebdesign

    thanks to share the tips.i read this and vary easy to role of table layouts in Responsive web design.I find myself using table layouts a lot more for this sort of thing…specifically a fluid cell that expands and contracts.Although I will have to try using the display: table; solution sometime.Thanks a lot for sharing them with us.

  • Tony Smith

    Thank you so much for great tips. I had to play around with it on my recent work.I think Responsive website design might turn out as a great way to progressively enhance even small budget projects for mobile devices.

  • Pingback: BlogBuzz March 16, 2013

  • Pingback: The Role of Table Layouts in Responsive Web Design | Webdesigntuts+ | Nelson Vale

  • http://www.bluesmokecreative.com/ Daniel Dogeanu

    I struggled with this a lot and at some point I realized myself that I could use display: table;. I would add a bit more to this. If you have, let’s say a nav, and you use glyph-icon fonts in your menu, you could use white-space: nowrap; in order to prevent the menu wrapping and then use media queries to reduce the font size to fit in the area allocated.


    nav {
    white-space: nowrap;
    overflow: hidden;
    }

  • http://www.timestips.com/ Piyush Kaila

    thanks
    i see table layout first time on mashable.com’s design.
    but i don’t know enough CSS so i can’t understand it.but now all my difficulties are solve.
    now i’m going to work on my blog http://www.timestips.com using table layout.

    thank you so much once again.