r/css • u/alexkopaleishvili • 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?
69
Upvotes
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-itemsthat was introduced later:25
u/snifty 6d ago
Think about how you would make a flag of Benin layout with flexbox. You will come to appreciate 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
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.
8
u/scott_in_ga 6d ago
grid
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-fitandauto-filloptions for repeating elements that give you a little more control vs flex.minmaxis also really cool (though of course you can replicate some of it with themin(),max()andclamp()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.
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 5d 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 5d ago
I'd say its normal, I even made a helper class (altho using tailwind):
.flex-centerthat does exactly that1
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