RFC: Pure Grids v4 (CSS Grid, `gap`, and auto-fitting layouts)
Summary
In Pure v4 we'd like to build Pure Grids on CSS Grid. The class names you use today (pure-g, pure-u-1-2, pure-u-md-1-3, …) would stay the same. On top of them we'd add:
- Built-in gutters with
gap, set by a single CSS custom property - An auto-fitting grid that makes responsive card layouts with no breakpoints
- Optional container-query units, so a component can respond to its own width instead of the viewport's
We'd like your feedback before we start. Please comment with what works for you, what worries you, and how you use Grids today.
Motivation
CSS Grid, gap, minmax(), container queries and subgrid are now supported in every evergreen browser. They give us a chance to extend Pure Grids in ways many of you have asked for, especially gutters, while keeping the small footprint and readable fraction class names that make Pure Grids easy to use.
Proposal
1. Grid container
.pure-g {
display: grid;
grid-template-columns: repeat(var(--pure-grid-columns, 120), minmax(0, 1fr));
gap: var(--pure-grid-gap, 0);
align-content: start;
}120 columns is the least common multiple of 5 and 24. Every existing fraction (fifths, the 24ths, and everything in between) maps to an exact whole number of columns.
2. Grid units
Units keep their names and map to column spans:
| Class | Spans |
|---|---|
pure-u-1 / pure-u-24-24 |
full row |
pure-u-1-2 |
60 of 120 |
pure-u-1-3 |
40 of 120 |
pure-u-1-5 |
24 of 120 |
pure-u-7-24 |
35 of 120 |
Responsive units (pure-u-sm-*, pure-u-md-*, pure-u-lg-*, pure-u-xl-*, …) work the same way at the same breakpoints.
3. Gutters
The default gap is 0, so layouts look the same unless you opt in:
.my-layout { --pure-grid-gap: 1rem; }You could also use a small set of preset classes, for example pure-g-gap-sm, pure-g-gap-md and pure-g-gap-lg (see the open questions).
4. Auto-fitting grid (new)
<div class="pure-g-auto" style="--pure-grid-min: 16rem">
<div>…</div> <div>…</div> <div>…</div>
</div>Items fill the row at no less than --pure-grid-min each and wrap on their own. You don't need breakpoint classes.
5. Container-query units (new, optional add-on)
A pure-g-container wrapper plus classes like pure-u-c-md-1-2 that respond to the wrapper's width rather than the viewport's. This would ship as a separate file (grids-container.css) so nobody pays for it unless they use it.
6. generateGrids()
require('purecss').generateGrids(units, options) keeps its signature and produces span-based output. It gets a new columns option. By default, columns is calculated from the units you request.
Migration guide
Most layouts that use only pure-g with pure-u-* fraction classes need no changes.
- Update Pure to v4 and include
grids.css/grids-responsive.cssas you do today. - Direct children of
.pure-gwithout apure-u-*fraction class will take up a full row. Add a fraction class (e.g.pure-u-1-2) to any child that should share a row. pure-uwith no fraction (content-width units): if you depended on a unit shrinking to fit its content, switch that container topure-g-auto, or give the unit an explicit fraction. (Open question below.)- Custom widths on units (for example
.sidebar { width: 30%; }on apure-uelement) should become a span:.sidebar { grid-column: span 36; } /* 30% of 120 */ - Alignment inside a row: replace
vertical-alignon units withalign-self(start,center,end). Replacejustify-contenton.pure-gwithjustify-items, or usegrid-column-starton a unit to offset it. - Reordering:
orderkeeps working. You can also usegrid-column/grid-rowplacement directly. - Gutters: if you add spacing with an inner wrapper (like
.l-boxin the docs) or with padding, you can leave it as is. Optionally, remove it and set--pure-grid-gapinstead. - Custom grids built with
generateGrids(): rebuild your CSS after upgrading. If you previously setincludeOldIEWidths, that option is no longer used and can be removed. - Test at each breakpoint you use, particularly layouts with mixed denominators in one row (e.g.
1-5next to4-5, or1-3next to7-24).
Rollout
- Publish an opt-in preview file (
grids-next.css) in a 3.x minor release so you can try it side by side before v4. - Update the docs and the example layouts on the website to use the new grid, gutters and auto grid.
- Ship v4 with a changelog that links to this migration guide.
Open questions (feedback wanted)
- Content-width units: how many of you use a bare
pure-uto size units to their content? Should v4 keep a content-width option, and in what form? - Gap presets: is
--pure-grid-gapenough, or would preset classes (pure-g-gap-sm/md/lg) help? What sizes? - Breakpoints: which responsive prefixes do you actually use? Should
xxl,xxxlandx4kstay in the default build or become opt-in? - Container queries: should container-query units be in the default build or stay a separate add-on?
- Anything else? Tell us about layouts you build with Pure Grids today that you think might not fit this model. Code samples are very welcome.
Source: pure-css/pure