DEV Community

Cover image for Ridiculously easy row and column layouts with Flexbox
Andrew Stuntz
Andrew Stuntz

Posted on

Flex Two Columns Ridiculously easy row and column layouts with Flexbox

Super easy responsive Row and Columns in straight up CSS

Grid layouts are the bread and butter of web development design and chances are you've reached for something like Bootstrap or Foundation to make your layouts a reality. But, like most of you, I don't have a fondness of the dependencies required to run Bootstrap or Foundation nor do I like the ding to my page load times.

In fact when I use Bootstrap for an application that I am writing, I really almost only ever use it for the grid layouts, sometimes I will use it for notifications or basic fairly sensible CSS defaults, but 90% of the time, all I want is the grid layouts.

I also don't appreciate only have options of splitting columns into 12 columns or less. It feels like you sometimes have to do some crazy work arounds to get columns in columns, or strange things like that.

How can we do grid layouts faster and easier? Flexbox is your answer. I think

Flexbox

At this point flexbox is pretty much everywhere. It's supported by all major browsers. It allows for much easier layouts and is supported by React-Native which means when I layout pages for React-Native I reach for flexbox first, but I have found my self reaching for flexbox first in web development as well.

In fact the last application I laid out, I did entirely with flexbox. I have found it that easy to use.

If you don't know too much about flex box. I like this page that gives a good run down of flexbox

Layout

First I wrap the entire page in a div.

<div class='some-page-wrapper'>
</div>
Enter fullscreen mode Exit fullscreen mode

Then I define a .row and .column class that can help with the layout.

.row {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
}

.column {
  display: flex;
  flex-direction: column;
  flex-basis: 100%;
  flex: 1;
}
Enter fullscreen mode Exit fullscreen mode

Now if we want a two column layout:

<div class='some-page-wrapper'>
  <div class='row'>
    <div class='column'>
      <div class='blue-column'>
        Some Text in Column One
      </div>
    </div>
    <div class='column'>
      <div class='green-column'>
        Some Text in Column Two
      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

The CSS looks like:

.some-page-wrapper {
  margin: 15px;
  background-color: red;
}

.row {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
}

.column {
  display: flex;
  flex-direction: column;
  flex-basis: 100%;
  flex: 1;
}

.blue-column {
  background-color: blue;
  height: 100px;
}

.green-column {
  background-color: green;
  height: 100px;
}

Enter fullscreen mode Exit fullscreen mode

What if we wanna add a third column? The HTML is easily updated to:

<div class='some-page-wrapper'>
  <div class='row'>
    <div class='column'>
      <div class='blue-column'>
        Some Text in Column One
      </div>
    </div>
    <div class='column'>
      <div class='green-column'>
        Some Text in Column Two
      </div>
    </div>
    <div class='column'>
      <div class='orange-column'>
        Some Text in Column Two
      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

And we get a third column added. That seamlessly nests itself in our row.

Now what if we want more complicated layouts?

We can just add more rows, that is pretty easy.

<div class='some-page-wrapper'>
  <div class='row'>
    <div class='column'>
      <div class='orange-column'>
        Some Text in Column One
      </div>
    </div>
    <div class='column'>
      <div class='blue-column'>
        Some Text in Column Two
      </div>
    </div>
    <div class='column'>
      <div class='green-column'>
        Some Text in Column Three
      </div>
    </div>
  </div>
  <div class='row 2'>
    <div class='column'>
      <div class='green-column'>
        Some Text in Row 2, Column One
      </div>
    </div>
    <div class='column'>
      <div class='orange-column'>
        Some Text in Row 2, Column Two
      </div>
    </div>
    <div class='column'>
      <div class='blue-column'>
        Some Text in Row2, Column Three
      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Or we can resize our columns.

To have a double column we can add a .double-column class. This can work with any sized column though, you can do 60/40, you can do 10/10/10/10/10/10/10/10/10/10, honestly any combination is possible here. You can do 1 X 100. Or 10 x 1, then 20 x 3, then 30 x 1. The options are endless!

On one layout, I added a large margin around my "column" and since the row will wrap down, I have one row with as many "columns" as I need. The columns were fixed width cards so they just wrap to the next line and flexbox neatly responsively wraps the cards down the screen.

.double-column {
  display: flex;
  flex-direction: column;
  flex-basis: 100%;
  flex: 2;
}
Enter fullscreen mode Exit fullscreen mode

This isn't very responsive though? We can add some responsitivity using media queries.

Just move the flex: 1 and flex: 2 into a media-query (size depends on application I'm just giving an option)

@media screen and (min-width: 800px) {
  .column {
    flex: 1
  }

  .double-column {
    flex: 2
  }
}
Enter fullscreen mode Exit fullscreen mode

At > 800px:

large responsive at greater than 800

At < 800px:

resonsive at less than 800

The final codepen, hint click the 1x or 0.5x buttons in the lower right corner to see the difference in the "responsive" layout.

Essentially we just blew the row/columns of bootstrap out of the water with 20 lines of CSS. We have a way to create row/column layouts quickly and since we use flexbox we barely have to worry about the layouts breaking, or anything going wrong. It's easily adapted to a wide variety of uses and allows a large amount of customizability. What do you think about flexbox? Have you tried it yet?

Another hidden benefit is that if I layout React components in this way, its pretty easy to layout React-Native components very easily to look similar.

I usually use SCSS in my projects so if you see something that isn't perfect CSS let me know!

Oldest comments (49)

Collapse
 
ben profile image
Ben Halpern

Thanks for the example-driven post. Super great resource.

Collapse
 
zeddotes profile image
zeddotes • Edited

Cool tutorial.

What if we wanna add a third row?

Think you mean "column". And the next paragraph, too, refers to "row" instead "column".

Collapse
 
drews256 profile image
Andrew Stuntz

Whoops. Thanks for the catch.

Collapse
 
drews256 profile image
Andrew Stuntz

Ohhh. I'll have to check this out.

Collapse
 
jbeetz profile image
J Beetz

Thanks for the great article and examples. I've just started reading about flex box on MDN. This article really helped me understand it.

Collapse
 
drews256 profile image
Andrew Stuntz

Yeah, I am glad it is helping! This is just one example of how to use flexbox though it is very powerful if you play with it a little bit. Flexbox has really helped me step up my front end skills. It makes things like centering and aligning trivial as long as you think about your layout a little bit before you put any code down.

Collapse
 
nickytonline profile image
Nick Taylor

Nice writeup. Here's some fun katas sites for Flexbox for those interested:

CSS Grid is starting to become mainstream and if you don't need to worry about legacy browsers, it's definitely the way to go for layout. Having said that, we've started using css grid at work, but Flexbox is still very useful.

I've started watching Jen Simmons' YouTube series. It's another great CSS resource. Here's a relevant video.

Cheers and looking forward to your next post!

Collapse
 
drews256 profile image
Andrew Stuntz

Thank you!

Yesss. I am so psyched about grid becoming more fully supported. I don't even consider myself too much of a front end expert and I look forward to getting to use grid. I will consider it more seriously the next time I work on something new!

Collapse
 
nickytonline profile image
Nick Taylor • Edited

👍

@wesbos has a great CSS Grid course if you're interested.

Yeah check out @wesbos ' JavaScript30.com. He also has a great free course thanks to Mozilla on CSS Grid. Check out cssgrid.io. If you enjoy listening to podcasts, Wes and Scott "El Toro Loco" Tolinski have a great one called Syntax. And lastly, he did an AMA on dev.to a while back that might interest you.

Collapse
 
majdrada profile image
Majd Rada

Thanks for referring the flex box zombies game, it is very helpful.

Collapse
 
dance2die profile image
Sung M. Kim

Thank you Andrew.

I've never thought about giving meaningful class names such as rows & columns.

It just made CSS much more readable.

Collapse
 
drews256 profile image
Andrew Stuntz

Haha, I know there are many people with many ideas about how to name classes and ID's in HTML and CSS. I have yet to use anything that I liked enough to stick with. So, I go with "sensible" and "meaningful" class names. But It sometimes still feels unorganized. One of the biggest reasons I like to use the above to layout pages, is that I can scope all my SASS to the outer level div, and that can help keep things organized. My opinions on class naming are evolving though, maybe I should write something about that next!

Collapse
 
dance2die profile image
Sung M. Kim

My opinions on class naming are evolving though, maybe I should write something about that next!

I'd love to read about it ❤️🕺

Collapse
 
orliesaurus profile image
orliesaurus

I use Bulma CSS for this kind of stuff - any other good libraries out there that support flexbox or simil?

Collapse
 
klausdonnert profile image
Klaus Donnert

... and wrapping it in a form will break it.

Collapse
 
burzumumbra profile image
Ronald Flores Sequeira

Great example, I would recommend a little grid framework, for the not that front end developer, that don't want to use bootstrap.
daneden.github.io/Toast/

Collapse
 
drews256 profile image
Andrew Stuntz

I've heard of toast, but I haven't used it. I'll dive in!

Collapse
 
longbeachlandon profile image
Landon Spicer

Great tutorial! I really appreciate it. Probably will change the way I build sites.

Collapse
 
caranocreative profile image
caranocreative

Thank you! I am new to web developing and I appreciate articles like yours that break things down in an easy to follow way.

Collapse
 
lildvlpr profile image
Nestor Zepeda

I used to despise CSS for how complicated I found to set up a simple grid structure but this article made it super easy to understand. Thank you for this! I will definitely be referencing this in the future.

Collapse
 
domitriusclark profile image
Domitrius

Definitely referencing this in my resource list my company is putting together!

Thanks Andrew :)

Collapse
 
drews256 profile image
Andrew Stuntz

This is the best feedback! Thanks!

Collapse
 
domitriusclark profile image
Domitrius

You've now inspired me to write up some cool stuff like this but for Sass. You da man.

Collapse
 
robincingh profile image
Robin

That's pretty straightforward challenge comes when you have nested columns/ equal height content column/ background images.

Collapse
 
paul_israel_o profile image
Paul Israel

You're a life saver... Thank you😊