O'Reilly

CSS Columns

By on  

One major gripe that we've always had about CSS is that creating layouts seems to be more difficult than it should be. We have, of course, adapted and mastered the techniques for creating layouts, but there's no shaking the feeling that there should be a better properties available. One of the newer CSS properties at our disposal is the column set of properties; these properties allow us to create columned containers without the hassle of floats, clears, margins, and more.

The CSS

CSS' column feature contains a few different properties that work together to create the desired column set:

  • column-count: number of columns desired
  • column-gap: gap between columns
  • column-width: suggests an optimal column width; not an absolute value that will be used, but will be the basis for the browser's own calculations
  • column-rule-width
  • column-rule-style
  • column-rule-color
  • column-span: allows elements to span multiple columns
  • column-fill: how contents are partitioned into columns

For the purposes of elegant styling, you'll at least want to use column-count and column-gap:

/* 3 columns of list items will display with a 10 pixel gap between columns */
ul.col-3 {
	column-count: 3;
	column-gap: 10px;
}

If you're looking to add column rules, those are easy to add as well:

/* 3 columns of list items will display with a 10 pixel gap between columns, gold column rule */
ul.col-3 {
	column-count: 3;
	column-gap: 10px;
	column-rule: 1px solid #fc0;
}

Specific elements will even span columns if you so choose:

/* Assume this HTML:

<div class="col-3">
	<h3>Heading!</h3>
	<div>Section 1</div>
	<div>Section 2</div>
	<div>Section 3</div>
	<div>Section 4</div>
	<div>Section 5</div>
	<div>Section 6</div>
	<div>Section 7</div>
	<div>Section 8</div>
	<div>Section 9</div>
	<div>Section 10</div>
	<div>Section 11</div>
	<div>Section 12</div>
</div>

 */
div.col-3 {
	column-count: 3;
	column-gap: 5px;
}

div.col-3 h2 {
	column-span: all;
	text-align:center;
	background: #eee;
}

Simply CSS columns -- exactly what we've been looking for!

Using CSS columns has its advantages: you don't need to do your own math, you can avoid modulus-based calculations on the server side for breaking based on content length, and if nothing else, you have a semantic way of creating columned layouts!

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

  • CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

Incredible Demos

  • Creating the Treehouse Frog Animation

    Before we start, I want to say thank you to David for giving me this awesome opportunity to share this experience with you guys and say that I’m really flattered. I think that CSS animations are really great. When I first learned how CSS...

  • MooTools &#038; Printing &#8211; Creating a Links Table of Contents

    One detail we sometimes forget when considering print for websites is that the user cannot see the URLs of links when the page prints. While showing link URLs isn't always important, some websites could greatly benefit from doing so. This tutorial will show you...

Discussion

  1. Alain Rivest

    This is awesome, looks nice in Chrome but it’s not working in Firefox… :(

  2. Sorry guys, forgot the browser prefix for Firefox; it’s there now!

    • Jacob Rask

      If you add an unprefixed version it will work in Opera and IE10 as well.

  3. Very cool, thanks for sharing! Side-note you should add a BufferApp share button to you site

  4. Mike Taylor

    You need to include non-prefixed properties too–Opera supports it prefixless. This is why preprocessors are a good thing. ;)

  5. Thomas

    This is nice for Text-Content, but you can’t really use it for “real layout” as alternative to grid systems – as it is not possible to declare which content elements should sit in which column.

    something like the following would be great to do with column layout, but doesn’t work as intended :(
    And alle the other CSS3 layout modules (Grid, Flexbox, Template, ..) are not ready, so we just have to keep using float or inline-block

    left column
    center column
    right column

  6. David

    Awesome – just like to point out the code:


    div.col-3 h2 {
    column-span: all;

    should be h3, as you used h3 in the HTML.

    Sorry to be a bugger!

  7. Rolf

    I tried using this in an ongoing project but in the end ditched it because of the lack of browser support (so you have to do proper fallbacks still keeping the design) and because (as mentioned) mainly works best for text-only layout. I would probably use it for stuff with emphasis on text and if the target device is a mobile piece.

  8. Hi David,

    This looks really promising, especially for text heavy layouts, although it would be nice if it was supported universally across all major browsers. Its cool that headings can be spanned across multiple columns without having the need to use z-index or absolute positioning. However until any of these methods become more mainstream, I think I’ll stick to floating my elements when considering cross browser compatibility.

  9. There’s a jQuery fallback for early versions of IE with ‘Columnizer’ as I mentioned on a blog article a few weeks back (http://wp.me/p1V1Ia-3W).

  10. Jeroen

    For some reason, in my two column layout, the two columns don’t start at the same line. The first one is placed one line lower. As a newbie, I can’t figure out what’s causing it. :-( Do you have any ideas? Thanks!

  11. hi
    tried your example with IE it doesn’t work, but when I paste your code in w3schools try it section. it works .. any specific reason ??

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Open Files from Command Line on OS X

    I'm as much of a fan of application UIs as anyone else but I'm finding myself working more and more from the command line lately.  Much of that is becoming obsessed with media manipulation but I'm forcing myself to use less UIs so that I...

  • Get Stock Quotes From Command Line

    When I conned my way into my first professional programming gig, I didn't really think much about money -- just that I was getting my foot in the door.  But as my career has gone on, I've been more aware of money, investing, and retirement.  I've recently...

  • Geolocation API

    One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...

  • Create an Image Preview from a Video

    Visuals are everything when it comes to media.  When I'm trying to decide whether to watch a video on Netflix, it would be awesome to see a trailer of some kind, but alas that isn't available.  When I'm looking to download a video on my computer,...

  • New:  Webdesigner News!

    A new and exciting website has recently been launched for web designers and developers. You likely spend hours every morning browsing through hundreds of posts on your RSS feeds, hoping to stumble across relevant stories. Webdesigner News was built to provide web designers and developers with...