CSS Layout – Grid vs. Flexbox
codecoda.com
codecoda.com
The way I understand the situation in Chromium is that they are slowly moving to a new layout system called LayoutNG [0]. The current grid implementation is in the old layout system however and they don't want to spend time extending it just to abandon it a few months later. There's already an ongoing work to support grid (GridNG) in LayoutNG [1] and once that's done, adding support for subgrid should be "fairly straight forward" [2].
[0] https://bugs.chromium.org/p/chromium/issues/detail?id=591099
[1] https://bugs.chromium.org/p/chromium/issues/detail?id=104559...
[2] https://bugs.chromium.org/p/chromium/issues/detail?id=618969...
- A Complete Guide to Flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
- A Complete Guide to Grid: https://css-tricks.com/snippets/css/complete-guide-grid/
https://www.youtube.com/watch?v=KOvGeFUHAC0
Seeing it like that really made me appreciate how much boilerplate Grid could let you cut back on.
2d -> grid
1d -> flex
For the resources, I recommend:
Flexbox: https://flexboxfroggy.com/
Grid is great for 2D layouts especially if you're going to shift stuff around (e.g. responsive versions of the layout).
With Flexbox, you can sorta do it but it always feels way too hard, even if your layout is convenient enough to be able to use the order rule.
Exactly!
body,
nav,
header,
article,
section,
figure,
form,
div,
header,
footer,
button,
.button {
display: grid;
}
.button is gross but included as older Chrome/Edgium have issues using grid/flex on proper buttons.Good question. Think of a price button (to choose yearly payment, instead of monthly).
----
$299/year upfront *
Less than $25 a month
cancel anytime
----
* The symbol would be smaller, the number bigger, lined up in a horizontal row.
Where I struggle is that grid can do most of what flexbox does in 1d but ... generally simpler if you're relying on breakpoints to change your layout.
But yeah, for structural layouts grids are better.
| HEADER | |Nav|Content|Sidebar| | Footer |
is more what we're talking about. You could do that, but it's not what flexbox was meant for. Try grid out. It will work easier for grid like layouts.
In grid you can define this layout in a single construct. Whereas with flexbox, you're defining several nested flex elements to get there.
You can think about the entire layout as a unit, rather than the horizontal parts first (sidebar/main content) then the vertical (header/footer) or vise versa.
I guess you can say the same thing about the infamous <FONT> tag. But if you want styling information directly in the HTML, you can still do it with the "style"-attribute, using marginally more syntax. e.g. <div style="display:table"> or <div style="display:flex; flex-direction:row"> instead of table <table> where html table semantics are not appropriate.
I do find flexbox a much nicer model than tables, but I guess that comes down to taste. A flex is basically like a table with either a single column or a single row, and a bit more fine-grained control.
[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Co...
I wish it was intuitive to me, so I could use it quickly and without turning my css code into a mess.
It's all based on rows and columns, that collapse based on screen width. Plus, utility classes for all the alignment positions.
You'll still run into the issue of way too many nested div's, but I find myself more productive than trying to flexbox everything by hand. ymmv.
> but just looking at the raw HTML always leaves me a bit confused how elements on the page will relate to each other.
Generally you shouldn't be able to determine layout from the raw HTML. HTML should be semantic. CSS determines layout and styling.
Suppose you have the following desktop layout:
ONE TWO THREE FOUR FIVE
and you want to turn it into the following mobile layout: O N E
TWO THREE
FOUR FIVE
With floats, you can achieve that by overriding only a few properties, primarily 'width'. But if you asked me to do it with either flexbox or grid, I'd have to think for a bit.Flexbox is good for things that need internal flexibility in one dimension. Grid is for things that have a relatively static, two-dimensional layout. Floats are perfect for things that need to shuffle across dimensions, and I don't see any reason to avoid using them. After all, it's not as if floats are un-semantic or anything.
* Float will alight "one" to the left or right with a gap next to it, while flex can stretch, center, or align the element differently.
* If the elements are different heights, they'll stack up very differently.
I need to apply justify|align|content|items in the browser to make it do what I want. I can't for the life of me remember what they do.
But the underlying principles are neat. Flexbox gives me a container and an arbitrary number of children of various types which I can then position to my hearts delight. And things tend to fall in to place without me over-specifying every case.
As long as I'm working along one dimension, that is.
> but just looking at the raw HTML always leaves me a bit confused how elements on the page will relate to each other.
You say it like it's a bad thing. Chances are the relations are flexible.
One does run in to issues when the Flexbox rules apply across the bounds of components (e.g. Vue or React components) which makes composition difficult. Can be avoided by clearly separating layout from the rest. The S in SOLID applies to CSS as well.
I found the Flexbox Zombies course to be helpful for that: https://flexboxzombies.com
It's free right now too.
Flex isn’t too bad and honestly really unlocks awesome abilities after you get used to it. Once you know flex, you can do the infamous “how to vertically center anything” move pretty easy.
Also, using order with flex is awesome. If you are using Bootstrap 4+ you can very easily use most flex properties responsively to do some crazy transforming layouts as well.
https://www.rachelandrew.co.uk/archives/2016/11/26/should-i-...
They're extremely good and quite fun.
> You can see how nicely the div elements position itself horizontally. But if you notice, it doesn’t entirely take up all the space within the container. There’s a gap at the end.
There's no example outputs or screen shots or preview panes or anything so no, I don't "see".
I guess I'm supposed to be copying this code by hand into a CodePen or a sample HTML page but that wasn't made clear at the beginning. I started reading expecting an article explaining something but it turns out that it's a half-tutorial.
Luckily, people have posted a bunch of great links in these comment so I'm going to go and read those instead.
With gap, making a gutter is trivial. And while gap _can_ be used with flexbox, the thing is that it's not supported by Safari[1], and who knows when it will be implemented.
Flexbox: https://youtu.be/fYq5PXgSsbE
Is the '12-grid arrangement' part of CSS or just a convention used by current web devs? [I'm a curious back-end dev.]
This comes from paper design, as usual, and I don't think 12 was the number. It's just a number that makes sense for desktop screens (where it began).
On a website with 2 pop-ups going straight to your face when you open it (and the cookie-related one isn't even GDPR compliant) >_<.