CSS Grid Areas
ishadeed.com
ishadeed.com
I definitely learned flexbox and thought to myself "this is good enough", and haven't really pushed myself to learn grids. It does seem like there's some reason to prefer grid [0], but it overall I've never needed to do anything that can only be done with grid (except data tables, for which I use actual tables).
I've only used grid for responsive image galleries like https://labs.jensimmons.com/2016/examples/image-gallery-grid... or responsive cards like https://every-layout.dev/demos/grid-cards/. `grid-template-columns` is good to learn. `grid-gap` is convenient. Flexbox seems to handle everything else.
But I'm still iffy about grid template areas. The article makes a case for easily adjusting layouts per breakpoint, or "for team members who might need to change a layout." Maybe I'm just not the target audience.
But what’s nice about it is that, if you want them, grid areas are extremely declarative. With non-area templating, you are defining the layout based on the items, and that can be challenging.
https://caniuse.com/mdn-css_properties_gap (no IE 11 support)
Nothing stopping you from using both as necessary, combined it's a very strong layout tool
A lot of the problems programmers seem to have with CSS probably stem from this misapprehension.
I imagine the slash in CSS grid is for similar syntactic reasons, because the syntax also admits hyphenated identifiers [1] (custom-ident), which precludes the use of “-”. (They could have used a range operator like “~” or “..” though.)
[0] https://en.wikipedia.org/wiki/Slash_(punctuation)#Numbering
[1] https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column...
They were conceived to float text (or other elements) around static elements. Think text floating around a picture, like this:
https://tco-forum-uploads.s3.amazonaws.com/original/3X/0/3/0...
There are a lot of articles, some really good but I read through two of CSS-tricks articles and that is all there is.
Some consequences of that are that you can easily adjust the order and such. And just need to change one elements style to adjust the whole layout.
Besides the already mentioned css-tricks articles, I recommend checking out the guide to flexbox from mdn[1] (in the side menu there is also one for grids) as well as the reference to “CSS flexible box layout”[2]. These helped me get a deeper understanding about the concepts behind flexbox and how the properties and their shorthands actually work.
[1]: https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layou...
[2]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexibl...
Grid Garden https://cssgridgarden.com
Flexbox Froggy https://flexboxfroggy.com
> If I need to position child items within the grid, I need to specify the line number for each item by using grid-column.
Stuck at what is 'line number' in this context? I tried to force my way through it hoping context would fill it in but immediately got stuck at fractions like '1 / 3' followed by '1 / 4'.
The “line numbers” (not a great name) are the column edges. So 1/3 goes from edge 1 (the left-most) to edge 3, spanning 2 full columns.
Just very cool.
The one thing about auto flow I wish I understood was how to detect, and fill, gaps. If I have some random collection of differently sized objects, how do I ensure that auto flow leaves no gaps? Or even detect that so I can add something?
That was just an objectively bad syntax choice, but probably keeping on track with CSS.
On a side note: a problem with CSS' claim to be a programming language is that the grammar is not obvious.
Because of how spaces are already used to separate values in CSS (not to mention our own regular intuition when dashes are involved), this:
grid-column: 4 somegridarea / 6;
grid-column: span 3 / 6;
grid-column: span somegridarea / span someothergridarea;
is much better than these: grid-column: 4 somegridarea-6;
grid-column: span 3-6;
grid-column: span 3->6;
grid-column: span somegridarea-span someothergridarea;
grid-column: span somegridarea->span someothergridarea;I've been using the site to plan my grid layouts for a couple of years now and it just makes spacing so much easier
Great article! Will be referencing it often.
I haven’t found a world where minmax or calc fixes this. hope someone has answers!
Then you can update the value of just that variable from JS, and the grid will reflect it.
Here's a very simple example: https://codepen.io/trescenzi/pen/vYqpjvL.
But I thought that using screen size media queries is an anti-pattern, and that we should be aiming for fluid design without arbitrary breakpoints.
There are two dimensions, and three parameters each - start, end, size. Then there are at least three ways to declare each parameter. And then... shorthand properties, what a mess.
I always find it very hard to read someone else's grids. No matter how organised the dev is, the lack of an idiomatic way to write means there's always a very high cognitive load for parsing.
Also the "-template" names are a waste of typing when plural/singular is already enough to disambiguate grid from items.
sometimes miss the days of table layouts.
i think flex and grid are fine if you stick to the basics. but when you start having nested elements and things like display: content. things can become unmanageable.
- align-* is inline axis.
- justify-* is block axis.
- *-content is aligning grid tracks (rows/columns).
- *-items is aligning individual children within their grid area.
Probably thought 1-indexed is more understandable to regular people.
https://developer.chrome.com/blog/reading-flow-display-conte...
N.b there's been a weird trend for blogs to remove publish dates and it really compounds the issue when the contents then use relative dates. At least this one has a tiny "last updated" timestamp at the bottom...?
isn't all of this solved more easily via flexbox?
Grid -> 2-dimensional layouts
That being said, I use Flexbox for pretty much everything.
Flexbox -> the children define the size Grid -> the parent is in control
Flexbox -> Content first, Layout second.
Grid -> Layout first, Content second.
This becomes more complicated when the number of divs is dynamic as flex determines position based on siblings. With grid you already have defined the “layout”, and you are just placing the div into the correct position.
Grid does that in both axis too. So although you may be able to kind of replicate the 1d version with flex, it becomes much harder to align in 2D.
I wrote an article about this: https://cssprinciples.com/3/grid/
.flex {
display: flex;
gap: 8px;
}I've never been in a situation where nesting grids makes sense. Nesting flexbox obviates the need for grid in most cases.
the process of creating responsive layouts): https://flexboxcss.com