368 points · by headalgorithm · 7y ago · 114 comments
I feel like flexbox was such a savior already that I'm still comfortable there.
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)
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.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
It seems to be about a mix of grids with flux instead of either/or.
You are right that I just haven't NEEDED it and without that I don't tend to grock things.
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.
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)
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.
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.
+---+ +---+ +---+
|1H | |4H | |7H |
| H | | | | |
+---+ +---+ +---+
|2B | |5B | |8B |
| | | B | | |
+---+ +---+ +---+
|3F | |6F | |9F |
| | | | | F |
+---+ +---+ +---+
To get this you had to either compromise semantics (use [1,4,7],[2,5,8],… table) or resort to JavaScript (read computed dimensions, find tallest and unify rest).Graphic designers use this pattern very often. Finally coders will not have to worry about it anymore, soon. After 22 years of CSS.
Which, of course, doesn't mean it's easy or even sane to implement in CSS...
Once you have to match more than one variable dimension across two axis (headers/bodies/footers laid in vertical axis matching their "siblings" in horizontal axis in our example) you need either JavaScript, broken semantics or subgrid. This is -- as I understand it -- raison d'etre of CSS Grid Level 2.
To cite just a recent example from yesterday's Hacker News front page: https://nationalparktypeface.com/
Fire up your browser's Responsive Design Mode and play around with the window width. The basics are generally easy to fix if you take a minute or two to spot check your layouts.
Probably you are not alone, but I'm sure you've just haven't spent enough time building complex or niche things in the browser. For simple brochure sites, CSS3's full feature set is an overkill. Use cases on the other hand are near infinite and there are thousands of developers out there needing unique combination of CSS solutions that you can't think of.
Rearranging content for example is a great way to reduce server rendering logic: just generate a bunch of <article> elements and the client will know how to display it. Even the user can make choices based on zoom levels what layout they prefer.
Even with JavaScript support it's nice to offload rendering logic to be handled by the browser which renders it directly on the GPU. If you do layout with JS, then there will be a function call doing expensive calculation every 16ms, that not ideal and CSS3 solved it for us in many cases.
I'm not pissing on W3C's (fantasai et al) work here. CSS is very much a work in progress as we all discover new UI idioms for digital media, and I'm a sucker for it. I just think CSS grids and flexbox is bordering on intellectual brain wankery, when the solution could be achieved much, much simpler, and in a way that doesn't send newbies into a world of cluelessness, doesn't question our ability to consume our own digital media in the future due to over-complexity, and retains the qualities of the web as a medium for simple self-publishing even for a layman.
Enter CSS.
Today's content is very different. Today complex applications that were 20 years ago bound to the desktop are (re-) created using Web technology.
This seems overkill for some apps like landingpages, however complex apps benefit from CSS that reacts to its context. Otherwise you would be very JavaScript heavy on those layout things and that is pretty much the essence: CSS3/4 pretty much did what JavaScript layout tools did but native.
Yes, if all you care about are able-bodied Western users consuming content on standard devices, then you can get away with just baking your style / layout into the markup like your someone hacking away back in the 90s. The moment you need to move beyond that, you'll start to introduce methods for abstracting style and layout that, eventually, will lead you to reinventing something pretty similar to CSS.
Edit: Look at the comments in this thread. They're mostly about "how flexbox works" and how to apply particular CSS grid features. Nobody actually authors content with the intent of structure/presentation separation.
Could be a while though, what is the bet that Safari will be last to get subgrid?
I think that since Google forked Webkit things have only made it into Chrome to then appear later in the other browsers. But you never know.
Also I often find that the number one reason for hard to style scenarios, is overly complected markup with deep div soups and other non-semantic container elements.
Supported everywhere, keeps markup to the bare minimum and doesn't smell like 1992 <table> based layouts.
Float can have undesirable behaviour regarding document flow and clearing afterwards. I try to use it just for floating things inside of text.
Position is just a bit illogical, really. Especially when playing with absolute. It puts you at the mercy of any parent element that just happens to be position:relative. I try to stay in the document flow whenever possible.
I like flexbox a lot. My biggest complaint is simply that it has a ton of rules and have to frequently look them up. It's a lot nicer to work in for 1D interfaces though. Especially in the age of responsive design.
I haven't had a chance to really dive into grid yet, but from what I've read it looks really useful for 2D layouts. It means not having to rely on the various column-based CSS frameworks for more complex sites. This will probably be my go-to in the future for main site layouts.
In my opinion however Flexbox and Grid have a decent advantage when it comes to responsive/fluid layouts. Grid and Flexbox scale by design while floats and position feel static.
Word of advice: don't even think about viewing the source on this very page. :P
CSS grid level 2 is a W3C draft design which, if accepted, will make its way into all browsers.