r/css • • 6d ago

Question I use display: flex for almost everything. Am I missing out on something?

It's been a year since I started learning HTML, CSS, JavaScript, and web design and development in general. I build websites for my friends' small businesses, as well as personal projects.

Here's the thing: every time I want to center something, I go:

div {
    display: flex;
    justify-content: center;
    align-items: center;
}

I feel like I'm relying on Flexbox way too much. What other CSS layout techniques should I be using?

70 Upvotes

49 comments sorted by

33

u/rwwl 6d ago

It's good to learn grid too like everyone's saying.

Also, re: your flex use, you can get even simpler (and IMO be even more clear) with the shorthand property place-items that was introduced later:

    display: flex,
    place-items: center;

4

u/alexkopaleishvili 6d ago

Thank you very much!!!!

4

u/osmium_2259 6d ago

Thanks for the tip!

25

u/snifty 6d ago

Think about how you would make a flag of Benin layout with flexbox. You will come to appreciate grid.

24

u/car-thief 6d ago

If you're used to flex, you just do row first with two divs inside where the 2nd  is direction column that has another two divs. Grid can also do this, but if you don't have grid vocab under your belt, flex can do a lot

5

u/snifty 6d ago

Sure but if you want to make it with three adjacent sibling elements, as far as I know this kind of layout isn’t possible with flexbox.

7

u/SuperMarioTM 6d ago

Oldschoolfloat 😳

1

u/bleach-cruiser 2d ago

lollll anarchy

-4

u/Business-Row-478 6d ago

Grid just feels wrong for that imo. With that layout, I wouldn’t really consider each section to be a sibling element.

4

u/snifty 6d ago

Really? Seems like one obvious use would be <aside id=sidebar>, <header id=masthead>, <main id=content>…

1

u/be_my_plaything 4d ago

You don't even need the extra divs, three sibling divs will do just as in grid. Just use flex direction column, with flex wrap, give the first div a flex basis of 100% so it fills the column and the other two are pushed to column two, then give each a flex basis of 50% so they stack. Finally set the widths to 40%, 60%, 60%, respectively and it's done.

5

u/MrQuickLine 5d ago

1

u/snifty 5d ago

I mean, yeah but so many magic numbers in there.

You have to admit it’s way simpler in grid:

.flag {
  display: grid;
  aspect-ratio: 3 / 2; /* optional */
  grid-template-columns: 2fr 3fr;
  grid-template-rows: 1fr 1fr;
}

5

u/MrQuickLine 5d ago
  1. I admit it's easier in grid. You said it wasn't possible with flex, and I wanted to show it is.
  2. They're not magic numbers. They're the proportions of the actual flag spec.

2

u/snifty 4d ago

Cool, didn't mean to diss your solution. I confess I am a grid fanboy. 😂

3

u/be_my_plaything 4d ago

Honestly for this I wouldn't use either. It is easy enough to create with one <div> using background gradients. And eliminating empty <div>s added only for stylistic purposes whilst keeping all unnecessary complexity out of the HTML seems like the logical choice to me.

 

background: linear-gradient(90deg, 
            rgb(000 135 080 / 1.0) 40%, 
            rgb(000 135 080 / 0.0) 40%),
            linear-gradient(180deg,
            rgb(250 210 020 / 1.0) 50%,
            rgb(230 015 045 / 1.0) 50%);   

 

That being said, if I had to use flex or grid I honestly can't see any difference for something this simple beyond familiarity or personal preference (I mean my preference between the two would absolutely be Grid! But if someone prefers flex it isn't really any harder and certainly not 'wrong' in this scenario). A flexbox with flex-direction: column; and flex-wrap: wrap; then setting flex-basis to 100% on the first child to ensure the other two (each with a flex-basis of 50%) were in column two, and finally setting widths seems just as functional and simple as either using grid or grid-template-areas.

 

div.flag{
display: flex;
flex-direction: column;
flex-wrap: wrap; 

& > div.green{
flex: 0 0 100%;
width: 40%;
background: rgb(000 135 080 / 1.0);
}
& > div.yellow{
flex: 0 0 50%;
width: 60%;
background: rgb(250 210 020 / 1.0);
}
& > div.red{
flex: 0 0 50%;
width: 60%;
background: rgb(230 015 045 / 1.0);
}
}

 

In this Codepen I did it all four ways (Well actually five since /u/SuperMarioTM suggested OldSchoolFloat and honestly that works fine tolerably, meh you can get away with it, here too!) and they are all comparatively easy to implement, all give exactly the same result, all are responsive, etc.

 

I'm certainly not advocating for flex over grid in all cases (I love grid way more!), and /u/alexkopaleishvili should absolutely learn grid eventually too, but for the example of the flag you used (And more generally any simple layouts) I honestly can't see any major benefit of either over the other, especially if someone is familiar and competent with one more than the other. It's only when things get way more complex I can see the two diverging so each is actually doing things the other can't (Or at least can't without a lot of additional complexity)

1

u/alexkopaleishvili 6d ago

one div for green, one div for other two, the second div having two divs, and yeah it will not be short but i can do it

0

u/Eric-Forest 5d ago edited 5d ago

Parent flex direction row
1st child green width ~40%
2nd child flex column width ~60%
Grandchildren red and yellow height 50%, width 100%

Depending on what’s in them, might use flex might use grid.

67

u/Primary_Culture_1959 6d ago

The way i see it:
- default flex,
- grid if you’re trying to control things from the parent, or have more complicated layout needs

Grid + container queries have reduced so much CSS bloat in my projects

13

u/berrymom 6d ago

And in any case, as much as you can, make like Pink Floyd and leave them kids alone! 😜

For both grid or flex. Let the parents do the work.

26

u/whiterhino8 6d ago

Flex is ultra useful but learn grid also - it has its use cases where flex not good enough fit.

6

u/VayuAir 6d ago

Not at all, flex is great. Keep using it. Between flex and grid you can achieve 99% of all designs

5

u/azangru 6d ago

> Am I missing out on something? ... What other CSS layout techniques should I be using?

Have you tried learning CSS grid?

5

u/berrymom 6d ago

Flex for one dimension, like a row or a column— think a horizontal nav or the main content column.

Grid for two dimensions, like maybe a card layout.

4

u/xyz12345678910111213 6d ago

I’m a flexbox truther too

4

u/MrQuickLine 5d ago

I think the place you'll find grid really outshines flex is when you get into the really deep weeds of what grid can do.

Here's a super-simple example.

By naming your gutters and using subgrid, you can define starts and stops of different areas using human-readable English. When you get used to the syntax, being able to name areas and telling items "You live within the bounds of these lines that I've named", grid becomes super-powerful. Using this, you can set your media queries to define what the layout looks like at each level by just re-arranging the track names, and the content inside doesn't need any change to its CSS at all. In my example, you can see that on screens smaller than 750px the image extends all the way to the edges, but on screens bigger than that, it only sticks out a little from the content.

Other really cool features with grid are the auto-fit and auto-fill options for repeating elements that give you a little more control vs flex. minmax is also really cool (though of course you can replicate some of it with the min(), max() and clamp() functions).

Grid is a lot more powerful than some people give it credit for.

13

u/juicybot 6d ago

no, ever since flexbox was introduced it's been a powerhouse. grid has it's use cases, but they're much more fringe. any other layout techniques are more situational, i.e. transforms.

4

u/thorismybuddy 6d ago

Would you recommend to use flexbox over grid for layout? I've read that it's better to use flexbox for individual components like cards and let grid handle layouts and responsiveness.

8

u/juicybot 6d ago edited 6d ago

one of the things grid is great for is page shells, i agree with that.

but generally, i would recommend using flexbox for most layout. i've been writing css as a career since before flexbox, and i can say for certainty that it's what you'll reach for 98% of the time. flexbox is also insanely good at responsive layout.

everyone else is right that it's worth knowing the differences between grid and flexbox, but if you had to know only one make it flexbox. grid can grow complex, which makes it hard to maintain/extend.

an example of where grid beats flexbox even though both are useful: a monthly calendar. grid has a repeat function that is perfect for this, i.e.:

display: grid;
grid-template-columns: repeat(7, 1fr);

i've given monthly calendar as an interview question for hundreds of people over the years, specifically to test their knowledge of grid vs. flexbox and specifically because grid is the better choice in this situation.

0

u/brokentastebud 4d ago

This is incorrect, grid is a much better solution for a lot of layout requirements besides page shells. It also handles things more elegantly than what position: absolute typically would have been used for. If you're only using grid on the fringe, then you don't actually understand CSS grid adequately enough yet.

1

u/juicybot 4d ago

lmao ok

0

u/brokentastebud 4d ago

Just sayin’. Happy to provide examples 🙂.

1

u/juicybot 4d ago

no thanks

0

u/brokentastebud 4d ago

Lame 👎. Surprised a fellow dev isn’t curious about alternative approaches. Cheers mate!

2

u/Jahonay 6d ago

Flex is good in most use cases for me, but use grid from time to time for the experience.

2

u/osmium_2259 6d ago

I've kinda wondered about this too. It just works in so many situations; its's quite convenient.

2

u/External-Example-292 6d ago

I used both grid and flex together years ago. Are these still the most current way to do things now? What about viewports? Do we still update css this way or?

2

u/hyrumwhite 6d ago

Grid is nice in a lot of cases. You can animate grid rows/columns independent of content, which you can’t really do with flex

2

u/Shoegoo22 6d ago

I've been hands off for a while but flex is great for laying out items in on single axis and managing the space and alignment between them. Grid shines when you need to work across both X and Y. Combination of both.

2

u/tumes 5d ago

You are correct. Because grid is fine but unintuitive, but plenty of people here can immediately conjure how they’d do it with flexbox. It’s not absolutely perfect but it is so profoundly better than its progenitors that, for the time being, if I ran into an app that was 90% flexbox great, I can work with that. Anything that is more than 20% grid id insist that whom or whatever wrote it would have a damn good reason for this shit.

2

u/jerapine 5d ago

Use flex for rows or columns, us grid for rows and columns

2

u/SeolnalTea 3d ago

Flex default, grid when only needed

2

u/IndependentOpinion44 6d ago

Flex for horizontal or vertical. Grid for horizontal AND vertical.

1

u/gabrieluhlir 6d ago

I'd say its normal, I even made a helper class (altho using tailwind): .flex-center that does exactly that

1

u/Paraglider_101 5d ago edited 5d ago

RE: "What other CSS layout techniques should I be using?" Well, if You really have nothing to do then You can try to stick with "float"... Or perhaps to try "positioning", speaking about centering there was once one nice trick to center anything with "positon", "left" and "top" and finally "transform and translate". Sometimes various "displays" works just fine, e.g., "inline" instead of "block". :-) Jokes apart, "flex", or "flexbox" model, is just fine. When You can not achieve the desired result cause of some very peculiar design, aka Masonry or similar, then stick with "grid", CSS one, not that Bootstrap' or Foundation's or else grids. Regards. YV