Saturday, June 15, 2024

Responsive CSS Structure Grids with out Media Queries — SitePoint

Must read

The muse for a lot of websites continues to be a format grid, whether or not that’s composed of Grid or Flexbox. On this excerpt from Unleashing the Energy of CSS: Superior Methods for Responsive Person Interfaces, we’ll see how each of those instruments present methods to create fluidly responsive format grids with out media queries.

Responsive Layouts with Grid

First up is probably my favourite of all of the options, due to its versatility and ease of use. Utilizing Grid, we are able to create a responsive set of columns that create themselves as wanted. We’ll present a single constraint — a minimal width that columns may be — which does double-duty as a kind of “breakpoint” earlier than column objects break onto new rows.

The next video demonstrates the conduct we’re after.

Right here’s all it takes to perform this responsive grid format, the place our minimal column dimension is ready to 30ch through a helper customized property. This rule directs the browser to create as many columns as will match which are at the very least 30ch extensive:

.grid {
  --min: 30ch;

  show: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, var(--min)), 1fr));

Since 1fr is the “max” worth of minmax(), the columns are additionally allowed to stretch to fill any leftover house equitably inside a row. So, if the out there house is 80ch and there are two grid youngsters, they’ll every take up 40ch. If there are three youngsters, the third might be on a second row, since 80 doesn’t divide equally into the minimal dimension allowed of 30.

The next CodePen demo offers a reside instance of a responsive Grid format.

See the Pen
Responsive CSS Grid format by SitePoint (@SitePoint)
on CodePen.

Responsive Layouts with Flexbox

We will accomplish an identical expertise with Flexbox. The distinction between the Flexbox and Grid resolution is that grid objects that move to a brand new row can’t increase throughout a number of grid columns. With Flexbox, we are able to direct the flex objects to develop to fill all remaining further house, stopping an “orphan” that seems with the Grid resolution.

On this code, as within the Grid code, the browser will create as many columns as will match the inline house with at the very least the --min dimension of 30ch. If we’ve got three objects and the third wants to maneuver to a brand new row, it is going to take up the remaining house because of the flex shorthand, which importantly units flex-grow to 1. It due to this fact has an identical conduct to 1fr typically:

.flexbox-grid {
  --min: 30ch;

  show: flex;
  flex-wrap: wrap;

.flexbox-grid > * {
  flex: 1 1 var(--min);

The picture beneath exhibits the ultimate, odd-numbered record merchandise spanning two columns, because of the flex-grow property.

Word: in each the Grid and Flexbox options, if we add a hole, that house might be subtracted from the calculation of what number of columns could also be created earlier than new rows are added.

Eager readers could have observed one other key distinction between these options: when utilizing Grid, the dad or mum defines the kid conduct. For Flexbox, we set the kid format conduct on the kids. The flex shorthand units, so as, flex-grow, flex-shrink, and flex-basis.

As an experiment, we are able to change the flex-grow worth to 0 and see how the objects will solely increase as much as the flex-basis worth. (Experiment with the CodePen demo beneath.) It’s essential to maintain flex-shrink to 1, in order that, finally — when the out there inline house is narrower than the flex-basis — the objects are nonetheless allowed to “shrink”, as this helps to stop overflow.

The next CodePen demo exhibits our Flexbox format in motion.

See the Pen
Responsive Flexbox Grid format by SitePoint (@SitePoint)
on CodePen.

The flex-basis property may be additional adjusted for this resolution to assign distinctive “breakpoints” for various objects. Since we’re setting that worth through the --min customized property, and Flexbox youngsters management their very own dimension, we are able to modify it with an inline type:

<li type="--min: 40ch">...</li>

The opposite record youngsters on this instance will nonetheless move round it and use the 30ch from the bottom rule, however the wider column successfully modifications the conduct.

Right here’s a CodePen demo of this code in motion.

See the Pen
Responsive Flexbox Grid format – adjusted –min by SitePoint (@SitePoint)
on CodePen.

Listed below are two different Flexbox methods that use flex-grow and flex-basis in fascinating methods:

  • Heydon Pickering’s Flexbox Holy Albatross, which breaks down from columns right into a single row primarily based on the dad or mum container’s whole width.
  • Heydon Pickering’s and Andy Bell’s sidebar format, which exhibits tips on how to power various Flexbox-based breakpoints for higher management of when objects wrap.

This text is excerpted from Unleashing the Energy of CSS: Superior Methods for Responsive Person Interfaces, out there on SitePoint Premium.

Supply hyperlink

More articles


Please enter your comment!
Please enter your name here

Latest article