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

Recent Features

  • LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() – this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • jQuery UI DatePicker:  Disable Specified Days

    One project I'm currently working on requires jQuery. The project also features a datepicker for requesting a visit to their location. jQuery UI's DatePicker plugin was the natural choice and it does a really nice job. One challenge I encountered was the...

  • Create an Animated Sliding Button Using MooTools

    Buttons (or links) are usually the elements on our sites that we want to draw a lot of attention to. Unfortunately many times they end up looking the most boring. You don't have to let that happen though! I recently found a...

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

  • OSCON Portland:  Conference  Discount!

    O'Reilly puts on the best web industry conferences in the world.  These conferences include Fluent Conference, Velocity Conference, and the upcoming OSCON in Portland, Oregon from July 20-24.  Open Source Convention (OSCON) is a conference that focuses specifically on open source developers and the tools and possibilities...

  • Follow Redirects with cURL

    I love playing around with cURL. There's something about loading websites via command line that makes me feel like some type of smug hacker, just like tweeting from command line does. I recently cURL'd the Google homepage and saw the following: I found it weird that Google...

  • Developers Have WordPress, Amateurs Have Squarespace, Professional Designers Have the NEW Webydo!

    Web design platforms have traditionally come in one of two varieties. There are the solutions like WordPress and Drupal that are incredibly powerful, but an understanding of web development and coding is required to be able to use those platforms effectively. On the other side of the...

  • Chris Coyierâs Favorite CodePen Demos II

    Hey everyone! Before we get started, I just want to say it’s damn hard to pick this few favorites on CodePen. Not because, as a co-founder of CodePen, I feel like a dad picking which kid he likes best (RUDE). But because there is just so...

  • GSAP + SVG For Power Users: Motion Along A Path

    Now that the GreenSock API is picking up steam, there are many tutorials and Getting Started guides out there to provide good introductions to the library, not to mention GreenSock’s own Forum and Documentation. This article isn’t intended for beginners, but rather a...