Home arrow HTML arrow Page 2 - Columns with CSS3 and HTML
HTML

Columns with CSS3 and HTML


In this first part of a two-part tutorial, I develop a couple of examples that show how easy it is to work with the “column” CSS property. It allows you to create grid-based web page layouts like the ones you can find in any printed magazine.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
November 02, 2011
TABLE OF CONTENTS:
  1. · Columns with CSS3 and HTML
  2. · Using the column CSS3 Property

print this article
SEARCH DEVARTICLES

Columns with CSS3 and HTML - Using the column CSS3 Property
(Page 2 of 2 )

In reality, breaking the main section defined within the earlier HTML5 document into two columns is a breeze, thanks to the functionality that the “column” property provides. The process is reduced to specifying the number of columns that will be used to split up the corresponding content, and the width assigned to the columns. It’s as simple as that.

It's time to show the CSS code that performs all of these tasks. Here it is:

#main {
    column-width: 250px;
    column-count: 2;
   
    -moz-column-width: 250px;
    -moz-column-count: 2;
   
    -webkit-column-width: 250px;
    -webkit-column-count: 2;
}

As the above code snippet shows, using the “column” property in a basic fashion is a straightforward process. As with many other properties included with CSS3, “column” requires you to specify some vendor prefixes, aside from the standard syntax, but this should be pretty familiar to you by now.

In this case, I decided to use the property to create two columns. Each of them has been assigned a width of 250px. With that set, the next logical step is to tie the previous CSS code to the web page defined earlier.

The following code fragment does that:

<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Using the 'column' CSS3 property (rendering 2 columns)</title>
<style type="text/css">
body {
    padding: 0;
    margin: 0;
    background: #fff;
    font: 0.8em Arial, Helvetica, sans-serif;
    color: #585858;
}
#wrapper {
    width: 800px;
    margin: 0 auto;
}
#main {
    column-width: 250px;
    column-count: 2;
   
    -moz-column-width: 250px;
    -moz-column-count: 2;
   
    -webkit-column-width: 250px;
    -webkit-column-count: 2;
}
</style>
</head>
<body>
    <div id="wrapper">
        <header>
            <h1>Using the 'column' CSS3 property (rendering 2 columns)</h1>
            <h2>Header section</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo.</p>
        </header>
        <section id="main">
            <h2>Left column</h2>      
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo.</p>
            <h2>Right column</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo.</p>
        </section>
        <footer>
            <h2>Footer section</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo.</p>
        </footer>
    </div>
</body>
</html>

Mission accomplished. If you test the above HTML5 document using a browser that supports the “column” property, such as Safari, Chrome and Firefox (not surprisingly, Internet Explorer is the bad boy again), this is how it should look when rendered on screen:

Didn’t I just tell you that creating web page columns with the “column” property was a snap? I guess that the image above speaks for itself. However, the property’s flexibility is such that it allows you to create any number of columns with the same ease.

If you want to see how to do this, in the next section I’ll be modifying the previous example. This time, it will break the content of the document’s main section into three columns, instead of the two that you just saw.

Creating a Three-column Web Page Layout

As I promised in the preceding segment, here’s the amended version of the previous example. It now uses “column” to split the content of its primary section into three sibling columns. Take a look at it:

<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Using the 'column' CSS3 property (rendering 3 columns)</title>
<style type="text/css">
body {
    padding: 0;
    margin: 0;
    background: #fff;
    font: 0.8em Arial, Helvetica, sans-serif;
    color: #585858;
}
#wrapper {
    width: 800px;
    margin: 0 auto;
}
#main {
    -moz-column-width: 200px;
    -webkit-column-width: 200px;
    column-width: 200px;
    -moz-column-count: 3;
    -webkit-column-count: 3;
    column-count: 3;
}
</style>
</head>
<body>
    <div id="wrapper">
        <header>
            <h1>Using the 'column' CSS3 property (rendering 3 columns)</h1>
            <h2>Header section</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo.</p>
        </header>
        <section id="main">
            <h2>Left column</h2>      
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo.</p>
            <h2>Center column</h2>      
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo.</p>
            <h2>Right column</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo.</p>
        </section>
        <footer>
            <h2>Footer section</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo.</p>
        </footer>
    </div>
</body>
</html>

Admittedly, the logic of this new example is easy to get, so I’m not going to waste your time explaining how it works. Instead, launch your browser and give the above web page a try. If all goes as intended, its main section should be displayed in the following way: 

Not bad at all, right? As you can see, the aforementioned section has been broken into three neat columns -- and best of all, no additional divs or floated elements were required to create this typical layout.

Even though this last example is per see pretty simplistic, it shows that the “column” property is a powerful addition to CSS3, which so far has been overlooked by many. Quite possibly, the property’s major drawback is the lack of support by Internet Explorer, which explains why it is used so little right now. However, with a proper fallback option, the usage of “column” could climb quickly to higher levels in the near future. As usual, time will provide the best answer to this question.

Final Thoughts

In this first part of this two-part tutorial, I went through the development of a couple of approachable examples, which showed how easy it is to work with the rather overlooked (at least until now) “column” CSS property. You saw for yourself how it allows you to create grid-based web page layouts similar to the ones that can be found in any printed magazine.

While it’s clear to see that the logic behind the property is easy to grasp, it’s worth mentioning that "column" comes with a few additional features that deserve a closer look. These include the ability to render customizable gutters and rules. With that idea in mind, in the final episode I’ll be discussing in detail the implementation of these facilities.

Don’t miss the last part!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
HTML ARTICLES

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials