new course
CSS grid landing page tutorial (36min)

Lesson info

Lesson info

Clone this projectDownload project assetsDownload lesson assets

Transcript

Flexbox. Grid. When on earth do we use each of these? Great question. Let's talk about when on earth we use each of these.

Flexbox. It gives us layout controls in one dimension. Here are a bunch of things inside a section. Flexbox, of course, lets us organize these things horizontally in one direction. It lets us organize these things vertically in one direction. And we can adjust justification and  alignment controls inside each of these. And the children themselves? We can override those settings, too.

That's flexbox.

What about grid? Grid gives us layout controls that can work in two dimensions. Here are a bunch of things inside a section again. Grid lets us organize wherever we want inside of our grid cells. Just like flexbox, we can adjust the positioning for these elements by making adjustments to our parent (our grid itself). And also like flexbox, we can override the positioning on the child elements, too.

That's grid.

And that’s where a lot of people might move on after saying “Enough. We get it. Flexbox for one-dimensional stuff and grid for two-dimensional stuff.”

It’s not that simple. Let’s bring in a pedestrian. And a car. And the city of San Francisco. Layout is a bit like transportation. If you start one place and your goal is to end in another, it might make sense to evaluate the most efficient form of travel.

In other words, sometimes it makes more sense to walk. Sometimes it makes more sense to drive. What if you’re in Union Square trying to get over to the Apple Store? Walking might make more sense here.

Maybe you need to go to Artichoke in Berkeley. Might make more sense to take a car or take public transit.

This is really similar to how we can think about flexbox and grid. Same idea here.

  1. Pretty easy to think of this as a grid, so we’ll draw out some grid lines. This would be great for grid. Could we create it with flex? Sure. But it might take a lot, lot longer.
  2. How about a magazine layout? Pretty easy to draw a grid around this, too. So this one, pretty straightforward in grid.
  3. Now what about this one? Generally in one direction. Pretty great candidate for flexbox.
  4. Vertical layout? Might be more straightforward with flexbox as well.
  5. Periodic table? Content organized along two dimensions? Probably grid.
  6. What about this layout? Kind of a newspaper layout. Grid would be great for this.
  7. How about a custom navigation bar with equally spaced links? Using flex, we can easily justify the space in between.
  8. John Nash cracking the Russian codes? Probably grid. Which, by the way, you can put a grid...inside another grid.
  9. Here’s where it gets a bit more nuanced. What if we have a footer with four main pieces? Logo, navigation, copyright blurb, and social buttons? Well it’s one-directional. But what about on a phone? That’s a lot to scrunch into such a narrow viewport. We can do this with flex, where each of these is set to 25% on desktop, then 50% each on tablet, and each at full width (100%) on mobile devices. This might be a good option.

    Or...just thinking differently here, we could use grid. We could use a 4-by-4. Then on tablet, we can make some adjustments by rearranging and having each block take up 2x2. There are a thousand ways to do this. Same thing on mobile — same thing when we move down to mobile. We can keep going to use our grid on each breakpoint as a guide.

    Some people might prefer to use flex for this; others might prefer to use grid.

And that’s the point here: if you’re not 100% sure after all of this whether you should be using grid or flexbox or a combo of both? That’s good. Because just like sometimes it makes more sense to walk or bike or drive or warp, grid is another option. Another tool in our design and development toolbox.

So. Flexbox: great for a lot of one-dimensional layouts. Grid: great for a lot of two dimensional layouts. But there are always exceptions. If someone tries to simplify with a 100%-of-the-time rule? We instead recommend trying it for yourself. Pick the tool that best suits what you’re trying to build — the way you want to build it.

But that’s our top-level comparison of flex and grid. We’re going to be adding a bunch of layout examples to our grid course. Check back as we build more and more using grid in Webflow.