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!

Track.js Error Reporting

Upcoming Events

Recent Features

  • Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?...

  • 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...

Incredible Demos

  • Dijit&#8217;s TabContainer Layout:  Easy Tabbed Content

    One of Dojo's major advantages over other JavaScript toolkits is its Dijit library.  Dijit is a UI framework comprised of JavaScript widget classes, CSS files, and HTML templates.  One very useful layout class is the TabContainer.  TabContainer allows you to quickly create a tabbed content...

  • Multiple File Upload Input

    More often than not, I find myself wanting to upload more than one file at a time.  Having to use multiple "file" INPUT elements is annoying, slow, and inefficient.  And if I hate them, I can't imagine how annoyed my users would be.  Luckily Safari, Chrome,...

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 all 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

  • Get Node.js Command Line Arguments with yargs

    Using command line arguments within Node.js apps is par for the course, especially when you're like me and you use JavaScript to code tasks (instead of bash scripts).  Node.js provides process.argv but that doesn't provide a key: value object like you'd expect: Bleh.  If you want to work with a...

  • OâReilly Velocity Conference â New York

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • Free Download: Font Bundle Featuring 17 Incredible Typefaces

    The only thing we love more than a good font, is a good free font. So we’ve combed the Web for some of our favorite free fonts, and gathered them here in a single download. You’ll find a variety of useful typefaces, from highly geometric designs...

  • OâReilly Velocity Conference â Amsterdam

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • CanIUse Command Line

    Every front-end developer should be well acquainted with CanIUse, the website that lets you view browser support for browser features.  When people criticize my blog posts for not detailing browser support for features within the post, I tell them to check CanIUse:  always up to date, unlike...