Flexbox in CSS to Make Columns

flexbox

Creating columns can be a tricky task in webdesign.

For many year webdesigners used tables to show content in columns. More recently, designers have been using CSS floats.

Flexbox is a CSS property that makes it possible display content in columns without the need of using floats or tables.

In this tutorial, we’ll show you how to get started with this useful CSS feature.

Step #1. HTML

Create an HTML file with the following code inside the body tag:

{codecitation html}<div class=”container”>
<div class=”column”>Column A</div>
<div class=”column”>Column B</div>
<div class=”column”>Column C</div>
</div>{/codecitation}

Step #2. CSS

Create a CSS file with the following code:

{codecitation css}.container{
width: 100%;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: flexbox;
}
.container > .column{
margin: 0 5px;
height: 50px;
background: gray;
color: white;
-webkit-box-flex: 1;
-moz-box-flex: 1;
-ms-flex: 1;
box-flex: 1;
}{/codecitation}

The prefixes in the CSS above are designed to add support for specific browsers:

  • -webkit- for Chrome and Safari
  • -moz- for Firefox
  • -ms- for Internet Explorer 10

Now, inside the head tag of your HTML file, add a link tag pointing to the CSS file:
{codecitation html}<link rel=”stylesheet” type=”text/css” href=”path/to/file.css” />{/codecitation}

Step #3. See it in action

Open your HTML file in a browser. The end result should look like the next screenshot:

flexbox

If you edit your HTML from Step 1 and add a new div with the column class, the four blocks will automatically use the same width:

flexbox

Author

  • Valentin Garcia

    Valentin discovered Joomla in 2010, and since then he has considered it as the best CMS. Valentin has been coding extensions and templates for Joomla for many years and truly enjoys helping people build their own websites with Open Source tools. He lives in San Julián, Jalisco, México.

    View all posts
0 0 votes
Article Rating
Subscribe
Notify of
guest

5 Comments
Oldest
Newest Most Voted
dhock
12 years ago

This doesn’t work for me in IE9, though it does in Chrome and Firefox. I read that it’s only supported in IE10. In IE11, you should remove the “-ms-” prefix. Also, is it -ms-flex or -ms-flexbox? I found the latter in another tutorial.

htmgarcia
12 years ago
Reply to  Name

Hi,

flexbox only works on IE10 and 11. Use -ms- preffix in IE10.

The syntax has been changing, please use the one works for the browser you want to support.
Regards

dhock
12 years ago

Thanks. Will it work for both IE10 and 11 if I include both “-ms-flex” and just “flex?”

htmgarcia
12 years ago
Reply to  DruSP

Please use the sample code from this post.
The thing with preffixes like -ms- or -webkit- is both works for specific browsers and are ignored for the others. Is a safe syntax.

CoryChass
CoryChass
12 years ago

I’m excited to see what flexbox will bring. I think I will improve efficiency and ease of coding once it’s fully adopted.

5
0
Would love your thoughts, please comment.x
()
x