This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Turning a flexbox into a grid using flex-wrap and align-content

1:47 CSS lesson by

Adding flex-wrap to a flexbox container allows the items to form a grid. The content can then be aligned and distributed along the grid using justify-content and align-content.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Adding flex-wrap to a flexbox container allows the items to form a grid. The content can then be aligned and distributed along the grid using justify-content and align-content.

Avatar
Chester

Pretty good lesson. My mind was a bit blasted at the grow and shrink concepts, but I'm sure I'll get it as soon as I start playing around with it.

Out of curiosity, how would you align the orphaned image on the last row to be aligned to the left, rather than to the middle?

Thanks guys!

Avatar
Rolando

Amazing series, thank you!

In reply to egghead.io
Avatar
Garth

Sadly there isn't a CSS only solution right now. One solution is to add extra items with zero height so they take up the same space but aren't visible.

Hopefully, in the future, there will be support for multiple ::after pseudo elements which will be treated like real DOM children.

In reply to Chester
Avatar
James

Short and to the point. Brilliant!

With flexbox and post-css, I'm kind of thinking I don't need Sass/Less anymore.

Avatar
Garth

It's my current favorite flavor of CSS.

In reply to James
Avatar
Fareez

awesome tutorial. in the grid example, what would you do if you wanted to remove all spaces between pictures?

Avatar
Garth

Change space-between or space-around to flex-start, flex-end, or center (depending on the justification you want).

In reply to Fareez
Avatar
Zhenyang Hua

It seems using align-items: center; similar to what align-content: space-around; does in this example. What is the difference when using align-items and align-content?

Adding wrapping to a long flexbox list can give you some nice grid-like features. In this example, I have seven images from Unsplash, each with a caption of the photographer.

Let's start by setting display: flex on the main container. In this case, it's the body tag. Let's fix the width. Right now, it's set using the width property. I'm going to uncomment this flex property to keep it more flexbox friendly.

Also, the flex-grow here is set to zero, so it doesn't get away from us. Now, we can see the list of images flow horizontally, which is fine. We don't really want it to go off the page. Let's add flex-wrap which defaults to nowrap.

Let's set it to wrap. There is one other option. We could also set it to wrap-reverse which causes the line breaks to flow up instead of down in this case. Notice the content bunches up to the top left corner.

If we want to spread it out a bit, we can add justify-content: space-around which we know will give us a nice space along the flex direction axis. It also affects the alignment of the orphaned item on that last row.

That doesn't really help us with our vertical rhythm. We can add align-content. It has values that we're already familiar with -- flex-start, flex-end, center, space-between, space-around, and stretch.

Stretch isn't going to show much because we have a fixed width and height. Align-content is only used with multi-line content. Make sure not to confuse it with align-items or align-self, as it won't do anything on single line content.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?