I love CSS Grid. I love how, with just a few lines of code, we can achieve fully responsive grid layouts, often without any media queries at all. I’m quite comfortable wrangling CSS Grid to produce interesting layouts, while keeping the HTML markup clean and simple.
But recently, I was presented with a unique UI conundrum to solve. Essentially, any given grid cell could have a button that would open up another, larger area that is also part of the grid. But this new larger grid cell needed to be:
Turns out there is a nice solution to it, and in the spirit of CSS Grid itself, it only involves a couple of lines of code. In this article, I’ll combine three one-line CSS Grid “tricks” to solve this. No JavaScript needed at all.
Here’s a minimalist UI example of what I needed to do:
This is our actual product card grid, as rendered in our Storybook component library:
Each product card needed a new “quick view” button added such that, when clicked, it would:
Hmmm… was this even possible with our current CSS Grid implementation?
Surely I would need to resort to JavaScript to re-calculate the card positions, and move them around, especially on browser resize? Right?
Google was not my friend. I couldn’t find anything to help me. Even a search of “quick view” implementations only resulted in examples that used modals or overlays to render the injected card. After all, a modal is usually the only choice in situations like this, as it focuses the user on the new content, without needing to disrupt the rest of the page.
I slept on the problem, and ultimately came to a workable solution by combining some of CSS Grid’s most powerful and useful features.
I was already employing the first trick for our default grid system, and the product card grid is a specific instance of that approach. Here’s some (simplified) code:
.grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, 20rem);
}
The “secret sauce” in this code is the grid-template-columns: repeat(auto-fit, 20rem);
which gives us a grid with columns (20rem
wide in this example) that are arranged automatically in the available space, wrapping to the next row when there’s not enough room.
Curious about auto-fit
vs auto-fill
? Sara Soueidan has written a wonderful explanation of how this works. Sara also explains how you can incorporate minmax()
to enable the column widths to “flex” but, for the purposes of this article, I wanted to define fixed column widths for simplicity.
Next, I had to accommodate a new full-width card into the grid:
.fullwidth {
grid-column: 1 / -1;
}
This code works because grid-template-columns
in trick #1 creates an “explicit” grid, so it’s possible to define start and end columns for the .fullwidth
card, where 1 / -1
means “start in column 1, and span every column up to the very last one.”
Great. A full-width card injected into the grid. But… now we have gaps above the full-width card.
Filling the gaps — I’ve done this before with a faux-masonry approach:
.grid {
grid-auto-flow: dense;
}
That’s it! Required layout achieved.
The grid-auto-flow
property controls how the CSS Grid auto-placement algorithm works. In this case, the dense
packing algorithm tries to fills in holes earlier in the grid.
minmax(20rem, 1f)
.align-items: stretch
causing cells to occupy 100% of the available row height.The result of all this is that the holes in our grid are filled — and the beautiful part is that the original source order is preserved in the rendered output. This is important from an accessibility perspective.
See MDN for a complete explanation of how CSS Grid auto-placement works.
These three combined tricks provide a simple layout solution that requires very little CSS. No media queries, and no JavaScript needed.
Yes, we do. But not for any layout calculations. It is purely functional for managing the click events, focus state, injected card display, etc.
For demo purposes in the prototype, the full-width cards have been hard-coded in the HTML in their correct locations in the DOM, and the JavaScript simply toggles their display properties.
In a production environment, however, the injected card would probably be fetched with JavaScript and placed in the correct location. Grid layouts for something like products on an eCommerce site tend to have very heavy DOMs, and we want to avoid unnecessarily bloating the page weight further with lots of additional “hidden” content.
Quick views should be considered as a progressive enhancement, so if JavaScript fails to load, the user is simply taken to the appropriate product details page.
I’m passionate about using correct semantic HTML markup, adding aria-
properties when absolutely necessary, and ensuring the UI works with just a keyboard as well as in a screen reader.
So, here’s a rundown of the considerations that went into making this pattern as accessible as possible:
<ul><li>
construct because we’re displaying a list of products. Assistive technologies (e.g. screen readers) will therefore understand that there’s a relationship between the cards, and users will be informed how many items are in the list.<article>
elements, with proper headings, etc..fullwidth
card is injected, providing a good natural tab order into the injected content, and out again to the next card.aria-live
region so that DOM changes are announced to screen readers.Although it isn’t demonstrated in the prototype, these additional enhancements could be added to any production implementation:
So, what do you think?
This could be a nice alternative to modals for when we want to reveal additional content, but without hijacking the entire viewport in the process. This might be interesting in other situations as well — think photo captions in an image grid, helper text, etc. It might even be an alternative to some cases where we’d normally reach for <details>
/<summary>
(as we know those are only best used in certain contexts).
Anyway, I’m interested in how you might use this, or even how you might approach it differently. Let me know in the comments!
The post Expandable Sections Within a CSS Grid appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.
This is the 3rd post in a small series we are doing on form accessibility.…
This is going to be the 2nd post in a small series we are doing…
Hey all you wonderful developers out there! In this post we are going to explore…
Hey all you wonderful developers out there! In this post, I am going to take…
These things called passkeys sure are making the rounds these days. They were a main attraction at W3C…
I spend a lot of time in DevTools, and I’m sure you do too. Sometimes…