Four years ago I posted “Don’t Overthink it Grids” and it resonated with quite a few people. Even back then, I thought we might have been at Peak Grid. Someone was promoting a new grid framework practically every week.
That article was my way of saying: “Fear not! You can make a grid yourself! You don’t need a complicated framework.” It might not have been quite as fancy, but that’s how I rolled. You float a couple of elements with some percentage widths and call it a day.
These days, if you are ready to jump to flexbox for layout, DIY grids are even easier.
This is how I normally do it:
<div class="flex-grid">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
</div>
.flex-grid {
display: flex;
}
.col {
flex: 1;
}
That supports any number of columns and they are automatically equal width and flexible!
Wanna have them break into a column for small screens? Easy:
@media (max-width: 400px) {
.flex-grid {
display: block;
}
}
Need gutters? You could add margins to the columns. You could add padding to the columns. I like the idea of using justification to create the columns, like:
.flex-grid-thirds {
display: flex;
justify-content: space-between;
}
.flex-grid-thirds .col {
width: 32%;
}
If you go the flexbox route, you also now have the ability to change the order of columns as needed, which can be great for keeping more important content higher in the source as well as responsive design reshuffling.
See the Pen Easy Flexbox Grid by Chris Coyier (@chriscoyier) on CodePen.
My point, again: it’s not required that you reach for a grid framework to build a grid. You can do it! This is also not to say that flexbox is trivially easy and you’ll never have any problems. There are plenty of edge cases and weird browser support things that you may run into if you start using more niche flexbox features. The stuff we looked at here is pretty vanilla though and I’d be surprised if you had problems.
More
Philip Walton has written more on the glory of flexbox grid systems.
To be fair, grid frameworks tend to be fairly robust, and plenty of teams find success in having predefined classes and recipes for building any type of grid they need. If you’re interested in leaning on a grid framework that is flexbox specific, here’s a few I know about: Frow, Flexbox Grid, and Gridlex.
Hahahaha, so funny: you float a couple of elements with some percentage widths and call it a day.
Thanks for the article, again some very usefull new things !
I’ve heard that using flexbox for the whole layout is not so recommended because the browser will put more effort to render it. Is it still true? If ever was true.
From the Google Developer blog: https://developers.google.com/web/updates/2013/10/Flexbox-layout-isn-t-slow?hl=en
In short, the old syntax was slow, the newer syntax is not.
Would you still recommend this if you are building larger projects?
Or to be more specific: When would you draw the line between using something like bootstrap, frow, etc. or your flex approach?
Thanks in advance!
The beautiful part of Flexbox is it’ll scale. You really don’t ever have to use a framework.
I may be a little biased, though. I don’t use frameworks regardless of the size of the project.
You know. Grids are only a small part of css frameworks.
I would say that the flex approach is most certainly the better approach for a larger project. Bootstrap is great for rapid prototyping or putting together a simple pamphlet website. But if you’re building a larger project like a web application, I would posit that Bootstrap has more bloat than you need, and you’re only using 10% of what it’s injecting into your site. Keep your CSS small by only writing the CSS you need, and using Chris’s method above is a great way to do that.
Great to see you doing this with flexbox. I put together a small sass library for flexbox grids. Inspired by Neat. http://codepen.io/jmacaluso711/pen/VvpZwG
Might be a bit overkill?
I would love to start using flexbox, but our main client is on IE11. Unfortunately, it appears there is only partial support. Any suggestions?
Unless your using min-height on the specific area, there should be no noticeable difference when using the code in this article.
http://caniuse.com/#search=flexbox
http://brolik.com/blog/when-to-use-flexbox/
To add to Andrew’s comments, check out Flexbugs that have helpful workarounds to a lot of the inconsistencies and issues: https://github.com/philipwalton/flexbugs
Thanks for this Chris. I agree, maybe the industry doesn’t need the big libs as much for this.
My only issue with
justify-content: space-between
for this type of abstraction is that it requires an actualwidth
to work instead of doing something likeflex: 1
, unless I’m missing something. Also, with the % based widths to draw in space for the gutters will create unpredictable gutter widths. So I prefer the margins between cols and then the negative margin trick on the container to bring the edges out. I can still usewidth
if I need it, but this also allows the use of thngs likeflex: 1
. Coincidentally, I’ve just developed my own “grid on flexbox” thing for a client just the other day. It’s not tried-and-true yet but shows the concept I’m talking about: http://codepen.io/bradwestfall/pen/yJVvBPSeems to run into the same gutter alignment issue that the DOFGs have.
Well, flex is always going to have that issue since
flex:1
is a relative and proportional to it’s siblings. But that wasn’t the point I was making.For your point, you can continue to use widths on my solution and it would work out nicely
In regards to the unpredictable gutter caused by the use of %, you should be able to control it a bit better by using ‘calc’.
Something like this:
This way, the gutter will remain at 15px.
The problem is more of a box-sizing issue it seems padding value is not added to whatever value flexbox determines for the column even with box-sizing border-box.
So as an example, I used 1,1,2 and 1,1,1,1,4. In that case things don’t line up, despite having the right sibling proportions. Unless you have zero padding+margin.
This is ok but it means gutters need to be added on the flex items children, either in the form of an inner column wrapper (woo extra divs) or just all direct children, maybe something like .col > * {}
I love Flexbox! The only thing that’s kept me from going 100% was browser compatibility, partially due to the three Flexbox standards that’ve been around. I’m giving it another deep-side-of-the-pool try now. Thoughts? CanIUse gave all green flags for current browsers except Edge.
You don’t really to worry about the old syntaxes anymore, thanks to autoprefixer
Regarding changing the flex grid to display block for small screens, would you also use display:initial to turn off flexbox, or otherwise using flex-wrap or flex-direction changes?
display inital would change display to inline on spans and other inline elements. might break the layout.
I really like Flexbox being used in various layouts but have this issue with it being used as a table.
Especially in the case when we want one of the column’s width to be determined by the width of its widest element.
Here’re my thoughts on this as 4 ways to approach this issue.
Generally speaking, grid systems (old school or flex) are not considered replacements for presenting data in table format
The problem I run into with this is gutter alignment.
Two rows: 1,1,2 and 1,1,1,1,4; will only align when there is no margin or padding. To align the gutters, it seems one has to jiggle around with the flex-basis property or use flex items purely as wrappers.
This seems to be the case, I really hope I am missing something obvious.
Yes, you need to play with flex-basis to get items to line up. Check this article out:
https://davidwalsh.name/flexbox-layouts
Flexbox is so awesome ! It saves time, its scalable, its for lazy developer, its easy to build grids according Layout and in combination with autoprefixer its solid for production. We use it in all projects for 2 years. I love it :)
Here’s a little fun with Sass for a somewhat semantic approach. HTML will have classes like 1/2, 1/3 and so on. Not sure on the cost of the selectors or browser compatibility, certainly a bit of fun :) Only using percents here but it certainly can be adjusted! This assumes a reset in place as well as box sizing goodness.
Was under the impression that classes can’t begin with numbers…
Classes in HTML are strings and can be any value, in CSS starting a class name with a number is an error (probably treated as a number literal) however if you pass them as string values to a class attribute selector you can enable HTML to make use of your number prefixed values. Whether its good practice or not I am not sure however when I see 1/4 on a container I kinda know whats going on :)
Here is another Flexbox based grid. (Disclaimer I’m the author :D ). Might be worth checking out.
https://splintercode.github.io/core-flex-grid/
Praying for the day when I don’t have to support people on IE8 and phones with 4-year-old browsers/operating systems . . . “well, it looks terrible on John’s iPhone 2!” :-(
You will still have plenty of issues with IE9 and IE10.
For the love of god, if you define the
flex: 1
value, don’t forget to spell it out for IEflex: 1 0 auto
. You’ll wonder why it has a hissy fit and nothing looks right,Grid usefulness was rather superficial even before the flexbox era; it’s essentially just a glorified
width/cols
calculation, which hardly justifies a standalone library.For people reading these comments and looking for a light and robust layout tool:
there is the old “float + block-formatting context” technique that works everywhere
Abusing overflow hidden for BFC and clearing is so dirty. Also compounding rounding issues on floats are not a real issue anymore, but if you still have in issue with that you can always use container relative floats (https://www.palantir.net/blog/responsive-design-s-dirty-little-secret). Some grid systems like Susy even provide isolated grids that use that method.
Hi, I’m looking for css generator maker to try my plugin to have feedback. https://github.com/onigetoc/FormToCSS
With this tool, you can generate CSS generator in minutes.
Demo: https://rawgit.com/onigetoc/FormToCSS/master/example/demo.html
Another flexbox grid system: http://philippkuehn.github.io/gridilydidily/
In your example, the columns should have
min-width: 0
to avoid stretching problems when the content is too wide.Yes! I ran in to this and fixed with
width: 0
(oroverflow: hidden
) – think I prefer the less-semantically-inappropriatemin-width: 0
though. Thanks.Any thoughts on “Don’t use flexbox for overall page layout”:
https://jakearchibald.com/2014/dont-use-flexbox-for-page-layout/
I like the simplicity of flexbox but is it really suitable for page layout?
It’s ready. https://developers.google.com/web/updates/2013/10/Flexbox-layout-isn-t-slow
Absolutley love flexbox for grids, but unfortunately the order properties mess with tab order and poses problems for accessibility. I believe the flexbox spec mentions this. You should try to avoid them for now.