I feel like flexbox was such a savior already that I'm still comfortable there.
I think Grid is a bit more complicated, more to learn. CSS is getting better but at the same time it's getting more complex in total.
Flex was finally FOR (I think it was for) layout, from the start. It's simple, works for a lot of use cases.
Grid is for layout, but serious layout.
It's good to have both.... well anything really considering the history.
I’m just happy to be able to layout things 2D in a way that isn’t totally bonkers and do weird contrived calculations to make everything fit in the viewport. My only wish remaining is a unit that is like fr for grid layout but can be the same value for both row and grid (so an equivalent to vmin for viewport)
Basically, a list of items arranged in a grid where the number of columns is dynamic depending on the width, items are wrapped dynamically from left to right, each column consumes the maximum amount of space, there's a fixed gap between columns, and the last row is aligned left, not stretched. I had to build one recently, and it was a perfect fit for grid.
Also, I think that tutorials for things like flexbox or grid should include fallbacks for older browsers. Because generally frontend devs simply copy the code from tutorial so if you don't include the fallback, they won't implement it.
> Also, I think that tutorials for things like flexbox or grid should include fallbacks for older browsers.
Well, a sensible fallback for that grid list example would be to inline-blocks with a fixed number of columns.
For instance, I recently made a component to be included on other pages at work. It had to look good no matter the size the page gave it. So they might put it into a 400px wide container, on a 1600px wide page. If I used vw or media queries to draw it based on page size, it would behave wrong.
Every time a float is used a Servo dev gets another grey hair.
Jokes aside, floats are Ok, but they make parallel layout from very hard to optimize, to no better than just using a single thread.
When hovered, the hidden content will appear via an animated dropdown. The height of the content varies as well.
Hopefully that makes sense.
If you're setting precise sizes in flexbox, or very specific flex values, you're probably in Grid territory. Ideally, flexbox is best when you want things to stretch/shrink to fit and you want the items to determine their own sizes. Grid is best when you have a specific layout/sizes in mind, and you want to impose that layout onto the items.
It does mention subgrid too.
An example I dealt with recently: I needed a header with logo / external link / social icons/ content / phone link. All to layout perfectly, and be mobile friendly.
With grid, I used this: (this is rough code to demonstrate the simplicity, I made a codepen demonstrating it properly. [0])
header {
display: grid;
grid-template-areas:
"logo content link social"
"logo content phone phone";
}
For each of the logo, content, etc... areas you use this to attach it to the specific location in the grid. .logo {
grid-area: logo;
}
.content {
grid-area: content;
}
etc...
You end up with exactly what you would expect: +------+---------+-------+--------+
| | | link | social |
+ logo + content +-------+--------+
| | | phone |
+------+---------+-------+--------+
For mobile, I wanted this: +---------------+
| Logo |
+---------------+
| Content |
+------+--------+
| Link | Social |
+------+--------+
| Phone |
+---------------+
Here's the only CSS needed for mobile. (or reverse this if you are doing mobile first layout) @media (mobile) {
header {
grid-template-areas:
"logo logo"
"content content"
"link social"
"phone phone";
}
}
Here's the html: <header>
<div class="logo" ></div>
<div class="content"></div>
<div class="link" ></div>
<div class="social" ></div>
<div class="phone" ></div>
</header>
The power to move content around with such simple and clean code like this is amazing.With flexbox you can use the "order" property to move stuff around a little, but not control the exact layout so cleanly and simply. There are tweaks for adjusting the widths of columns and heights of rows, but this is generally all that is needed to get the layout with grid.
There is also grid-gap [1], something not possible in flexbox yet. (though Firefox supports grid-gap with flexbox)
Since IE is no longer supported (or even considered a browser by MS, but a compatibility layer) what kind of site do you work on that is so heavily trafficked by primarily IE?
It seems to be about a mix of grids with flux instead of either/or.
But you are right, something like a gallery of images (unknown number of items that need to flow), flex seems more useful.
You are right that I just haven't NEEDED it and without that I don't tend to grock things.
dl {
align-items: baseline;
display: grid;
grid-gap: 1.5ex 1.5em;
grid-template-columns: fit-content(12ch) 1fr;
}
dl dt {
font-weight: 600;
grid-column: 1;
}
dl dd {
grid-column: 2;
}
This will put all your terms in the first column and all your descriptions in the second. All aligned nicely and creates a new row even if you have two `<dt>`s or `<dd>`s in a row. A flex solution would quickly fail once the terms get irregular.① Something that can be achieved with float, fancy margins or a couple of other related techniques (with caveats, inside this markup structure, such as needing both dd and dt to be inline, since `display: run-in` died):
Key: Value, even on
multiple lines
A long key: Value, even
on multiple
lines
② A flex approach: Key: Value, even on
multiple lines
A long key: Value, even
on multiple
lines
③ A grid approach, avoiding wrapping keys: Key: Value, even
on multiple
lines
A long key: Value, even
on multiple
lines
④ A grid approach, wrapping keys (and you’d better hope the key is wrappable): Key: Value, even on
multiple lines
A long Value, even on
key: multiple lines
Now as it stands, your fit-content approach lands part-way between ③ and ④, wrapping the terms where possible, but extending the column’s width if necessary (e.g. if you use a long, unbreakable word, which experience tells me will be a URL and thus insanely long). Yet I would argue that in most cases of unknown content, ① or ② are better, most commonly ①. Wrapping terms is generally a bad idea (especially if there is no border in the grid), and if arbitrary content can end up in the term you’re likely to get pathological cases, like URLs in the term.(I say all this as one who switched his known-content-that-doesn’t-trigger-these-cases <dl>s to `display: grid` over a year ago. But for arbitrary user content, I’d be more likely to go back to approximating ①, probably with markup other than <dl> now as well, to avoid problems related to it.)
This approach to handling outliers (break out of a rigid grid, wrap, extend all, &c.) is worth thinking more about. In the article this thread is about, I am actually not convinced that having the card internal elements lining up is a good thing. Here’s the problem: the depiction of its appearance when they don’t line up is deceptive, and not how you would realistically implement it. Without subgrid, you’d be using `display: flex; flex-direction: column` on the cards, and that’s what’s depicted; but what the depiction lacks is `flex: 1` on the content, so that the header and footer take the least space possible, rather than themselves flexing as well. If you fix that, the not-lining-up case no longer looks terrible; I’d say that at that size then it’s six of one, half a dozen of the other; and for larger grids or more pathological cases, it’ll very arguably be nicer.
Grid is just the layout (columns/rows), not entirely how text is handled. 3 & 4 are no different with grid, only word-break/word-wrap with forced/limited widths.
And 1 & 2 seem disjointed and hard to read (ie, break long held rules for readability), I don't see the value arbitrary space after the key. 1 & 2 would easily be better suited to this:
Key:
Value, even on
multiple lines
A long key:
Value, even
on multiple
lines
Or this: Key: Value, even on
multiple lines
A long key: Value, even
on multiple linesGrid requires no/fewer wrapping elements, solid outcomes (ie, code matches result more often than other layout methods from the start) and lots of minor properties for edge use cases. And this is just a summary of the benefits.
I think back to the early days (circa 2000 or so), before CSS positioning and float hacks were even a thing, working as the lead webdev at Upromise, meticulously hand-crafting complex layouts with nested HTML tables... you could nest them 5 or 6 levels deep before running into noticeable render performance issues in what must have been, what, IE4 and NN4? It really has been a couple decades, hasn't it. Kinda wish every FE dev had to follow the path the industry took to get where we are now...
/ramble
I think this is how web tech works best, when you see the best way coming down the pipe, don't waste time on hacks. But I admit, floating was the only way to make columns for far to long.
I know grid is taking a long time to take root. I primarily only use grid-template-columns, grid-gap and areas or really only needed for major/complicated layout.
Why not table with inline width=95%/align=center
No more floats, no more percent widths, no more weird nth child tweaks for positioning.
Add rems to the mix and responsive design has never been simpler. For grids you can just change from say a 3 column grid to a single column on mobile for most cases.
And the best part, no bootstrap.
Articles about flexbox have the same problem by the way.
None of my projects have an audience that includes 90 years olds using ie6 on windows xp. I assume my users are on some chrome derivative that is fairly up to date.
I'm not going to use this until it's well supported, but I'll have no qualms going so.
I certainly am not worried about anything I've used flex or grid for... they've been out for quite awhile now.
I'm not sure why we are all trained to think that anything we build has to be compatible with old browsers that essentially no one is using.
For most projects, I don't see a point unless you have some really specific requirements.
I think similarly, with the caveat that anything that might be useful to me at work (current or future day jobs) needs to consider IE11 because there are a lot of people out there stuck using that (we work with banks a lot, while it is increasingly common for recent-ish Chrome to be available too it is not close enough to ubiquity for comfort). This includes anything that I might want to log into myself from a client's site, where I might not be able to use my own device, not just that I might expect/want them to use.
> I assume my users are on some chrome derivative that is fairly up to date
Be careful just testing in Chrom{e|ium}. We are in danger of heading into what is effectively another period of browser mono-culture, especially now MS have thrown in the towel. I'd at least support "recent Chrome/Chrome-ish and recent Firefox". While chants of "the next IE6" are a bit hyperbolic, if a mono-culture, or something close to one, forms it will create future issues no matter how good are the intentions of the controller of the "winner".
I'd like to support Safari too but unless Apple provides a way of running that locally without having to buy their hardware or buy their OS then fight to get it running in a VM that isn't going to happen outside of commercial projects where someone else is paying for the resources to allow it to happen.
I meant Webkit-based browsers and Firefoxes that are over 3-5 years old. What about Android phones with firmware without updates? What about smart TVs?
> I'm not sure why we are all trained to think that anything we build has to be compatible with old browsers
Because compatibility was one of the ideas behind HTML. Because there are less developers than users so it is easier to solve compatibility problems at their side.