Ridiculously easy row and column layouts with Flexbox
Tags: css, html, javascript, webdev
Published: 2018-08-10 20:29:05

---
title: Ridiculously easy row and column layouts with Flexbox
published: true
description: Write really easy row and column layouts with Flexbox
cover_image: https://thepracticaldev.s3.amazonaws.com/i/c76o5bphqwjag8d940f5.jpg
tags: CSS, html, javascript, webdevelopment
---

# 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](https://caniuse.com/#feat=flexbox). 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](https://css-tricks.com/snippets/css/a-guide-to-flexbox/)

### Layout

First I wrap the entire page in a div.

```html
<div class='some-page-wrapper'>
</div>
```

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

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

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

Now if we want a two column layout:

```html
<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>
```

The CSS looks like:

```css
.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;
}

```
{% codepen https://codepen.io/drews256/pen/zLerNx %}

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

```html
<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>
```

{% codepen https://codepen.io/drews256/pen/djaYKo %}

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

{% codepen https://codepen.io/drews256/pen/djaYKo %}

Now what if we want more complicated layouts? 

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

```html
<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>
```

{% codepen https://codepen.io/drews256/pen/WKPwxN %}

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.

```css
.double-column {
  display: flex;
  flex-direction: column;
  flex-basis: 100%;
  flex: 2;
}
```

{% codepen https://codepen.io/drews256/pen/djaMWj %}

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)

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

  .double-column {
    flex: 2
  }
}
```

At > 800px: 


![large responsive at greater than 800](https://thepracticaldev.s3.amazonaws.com/i/vzblza9096iap76a55ju.png)

At < 800px:

![resonsive at less than 800](https://thepracticaldev.s3.amazonaws.com/i/g4vz31amzqbpn5duuu5l.png)


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

{% codepen https://codepen.io/drews256/pen/bjzpvd %}


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!