Navigation

How to create horizontal scrolling using display:table-cell

By Ezequiel Bruni | How To | Feb 25, 2014

Horizontal layouts of all kinds have intrigued me ever since I found out you could do that. I don’t know exactly why I’m fascinated with them… maybe it’s just my inner rebel talking. Whatever the reason, I’m just about ready to declare vertical scrolling obsolete, passé, and soooo last millennium.

Okay, that’s not really what I mean. Still, with the sudden increase in the amount of touch screens lying around, it becomes more difficult to assert that “up and down” are our best options. “Right and left” have become viable directions for content placement, as long as you’re not dealing with substantial volumes of text.

I never bothered to really build any horizontal layouts, though. The technical problems and limitations always seemed to outweigh any stylistic or navigational benefits there might be. That was before, however; and this is now…

I came across the technique described in this article the way I usually come across things: by trying to do something else entirely. I was attempting (you can laugh) to create a CSS grid framework based on display: table-cell (okay, stop laughing now).

Well, for reasons that now seem obvious, it didn’t work. You try making a responsive image grid with the table-cell property. Go ahead, I’ll wait.

Simply put, table cells are designed to form a single, horizontal row. (I said stop laughing!) That’s what they do, and they don’t like it when you try to make them do anything else. I gave up on that project. A few weeks later, though, I was considering redesigning my portfolio again.

I thought it would be nice to put all of my projects on one page. I considered several organizational solutions for displaying my web, writing, and photography projects, and came up with this: I want to display these three categories as horizontally scrolling rows of thumbnails.

That’s when it hit me: “Table cells would be perfect for that. Also, you can vertically center things inside them! I’m so smart it hurts!” [Some dramatization here.]

I haven’t gone and redesigned my site yet, instead, I coded up the two examples of my technique that are in the .zip file linked to at the bottom of this article.

 

Making it work

So, to give you a visual, here’s a demo I’ve worked up.

Here’s how each row is marked up:

<div class="horizontal">
<div class="table">
<article>
<h3>Project Title</h3>
</article>
<!-- Repeat this part as many times as necessary. -->
</div>
</div>

From there, the CSS required to make it work is simple enough:

// This container element gives us the scrollbars we want.
div.horizontal {
    width: 100%;
    height: 400px;
    overflow: auto;
}

// table-layout: fixed does a lot of the magic, here. It makes sure that the "table cells" retain the pixel dimensions you want.
.table {
    display: table;
    table-layout: fixed;
    width: 100%;
}

// Arranging your content inside the "cells" is as simple as using the vertical-align and text-align properties. Floats work, too.
article {
    width: 400px;
    height: 400px;
    display: table-cell;
    background: #e3e3e3;
    vertical-align: middle;
    text-align: center;
}

// Some styling for contrast.
article:nth-child(2n+2)
{
    background: #d1d1d1;
}

Some horizontal layout techniques require the container element (div.horizontal, in this case) to have a defined pixel width equal to the combined width of the elements it contains. Other techniques require display: inline-block; I’m not a fan of this technique. With table-cell, just keep adding elements whenever you need to, and you’re good to go — it’s perfect for use with a CMS.

 

Making it full-screen

Okay, the other kind of horizontal layout is the full-screen horizontal layout. Creating this with the table-cell property requires some JavaScript. I used jQuery to speed things up. The JS requirement might make this technique more situationally useful, but it’s still cool.

Here’s a working demo.

The markup is similar:

<div class="horizontal">
    <div class="table">
        <section>
            <h1>Full-Screen Horizontal Layouts</h1>
            <p>Made with <code>display: table-cell;</code></p>
            <p>By Ezequiel Bruni</p>
        </section>
        <!-- Repeat this part as necessary. -->
    </div>
</div>

Here, however, it’s just one “row” that’s been made to fit the size of the browser window. Each <section> has, in this case, also been made to fit the browser window.

Here’s the CSS:

// Don't touch this part. It helps.
html, body {
width: 100%;
height: 100%;
overflow: hidden;
}

// In this case, I didn't want a scrollbar, so I used overflow: hidden. The container element is more essential than ever, though. The body element will not do.
div.horizontal {
    display: block;
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: static;
}

.table {
    display: table;
    table-layout: fixed;
    width: 100%;
    height: 100%;
}

.table > section {
    width: 1600px; // The width is based on my monitor. It's replaced by jQuery anyway. Percentage widths do not work.
    height: 100%;
    display: table-cell;
    background: #e3e3e3;
    vertical-align: middle;
    text-align: center;
}

As stated above, percentage widths do not work. Pixel widths are required. If you want to make each section fit your window dimensions, you’ll need to do it with JavaScript:

$(window).load(function() {
    var vWidth = $(window).width();
    var vHeight = $(window).height();
    $('.table > section').css('width', vWidth).css('height', vHeight);
});

$(window).resize(function() {
    var vWidth = $(window).width();
    var vHeight = $(window).height();
    $('.table > section').css('width', vWidth).css('height', vHeight);
});

You’ll notice that I also added the height. Well, that’s for Firefox. Firefox doesn’t play nice with percentage heights on the table-cell elements (incidentally, Firefox also throws a hissy fit if you make cells relatively positioned, and place absolutely positioned elements inside them).

Well, that’s my technique for horizontally placing content. You can download the source files here.

 

Have you designed a horizontal site? Have you used a different technique for horizontal scrolling? Let us know in the comments.

Featured image/thumbnail, sideways image via Shutterstock.

Share this post
Comments (no login required)
  • tjedison

    Do you know any way to force the scrollbar to display cross-browser and cross-platform? It seems many modern browsers have the scrollbars that only appear when you are actually scrolling.

    • cmegown

      You can use overflow: scroll to get both the horizontal and vertical scrollbars to display even if the element doesn’t scroll right away. If you just want the horizontal scrollbar use overflow-x: scroll, or inversely, overflow-y: scroll to receive the vertical scrollbar.

      This will work on all desktop browsers, as well as some mobile browsers, but other mobile browsers often don’t actually show the scrollbar until the user starts scrolling.

  • Keith

    A very exciting technique. The full-screen version seems to get a little weird when the browser window is resized on any slide but the first one, though.

  • Guest

    Ok, but… why?

  • Adrian

    Another way you can do horizontal scrolling in completly responsive way is display: inline-block; and white-space: nowrap for the parent element. In this way you can add width: 100% to the child elements and they are always have the right width even in window resize without a single line of JS. However this technique has its downsides as you loose the posibility to vertical center the item content.

    HTML:

    Title
    Title
    Title

    CSS:
    .container {
    font-size: 0; /* Required because inline-block elements have space between them. Other possible solutions for the problem: http://css-tricks.com/fighting-the-space-between-inline-block-elements/ */
    overflow: auto;
    overflow-y: hidden;
    white-space: nowrap; /* actually this line of code does the “magic” */
    width: 100%;
    height: 100%;
    }

    .container > article {
    display: inline-block;
    font-size: 16px; /* Reset font-size back to normal */
    vertical-align: top;
    width: 100%;
    }

  • http://tzi.fr/ tzi

    Hi

    Ezequiel!
    Thanks for sharing this article.

    You can do the full screen technique without JavaScript. At least, it would be great for the default value, to be:

    .table > section {
    width: 100vw;
    height: 100vh;
    }

    Cheers,
    Thomas.

  • cameronolivier

    Nice article. I always feel a little dirty making divs behave like tables.. Feels like a Gargamel-esque tricksy-ness, but maybe that’s just me. All that aside, it works well, so who cares.

    2 things though – one is the mouse scroll isn’t automatically invoked for horizontal scroll, which is disappointing.
    Secondly, couldn’t you use 100vw with JS as a fallback? I *think* it should work – it seems more reliable than percentages as a ‘relative’ unit.

    • EzequielBruni

      See? You learn something new every day. I didn’t know about “vw” or “vh”. They’re new values to me. Gonna have to try that out.

      As for the mouse scrolling thing, yeah… pretty sure that needs to be done with JS. I wanted to focus on layout techniques for this article, not behavior.

      • cameronolivier

        Yeah, I hear you – I did a horizontal scrolling site a while ago, and needed to add a JS plugin to help with the scroll. The ‘vw’ and ‘vh’ I actually stumbled across while we were doing a hiring run at our company :) was going through an applicant’s code and was like- what’s this?! – best find ever :) hehe

  • kyle

    Thank you! I added scrollable thumbnails to the jQuery slider and this worked perfectly. I tried tons of other examples with no avail.