Skip to: Content or Footer

Aligning multiple column content using subgrid

Introduction

It’s been a “quest for the holy grail” kind of affair for me for many years… how to align different height columns within a grid item. We’re there at last using CSS subgrid, mostly. Currently as of writing (May 2023) only Firefox and Safari have support.

caniuse.com - table of browsers able to use CSS subgrid
caniuse.com – subgrid

Using @supports to test availability

If you’re using a browser capable of interpreting subgrid you’ll see the results below. If not you’ll see an error message. Not everyone has access to Safari (Apple only) but you can download Firefox and give it a try until the Chromium-based browsers ‘catch up.’

This browser does not support subgrid, please check the caniuse.com list of browsers that do support it.

Battlestar Galactica

A group of humans flee the Cylons, a race of cybernetic lifeforms, in search of a new home.

Stargate SG-1

A team of military and scientific personnel travel through the Stargate, an ancient portal that connects Earth to other planets.

Star Trek – The Original Series

The crew of the USS Enterprise explores the galaxy, seeking out new life and new civilisations.

Star Wars – Episode IV “A New Hope”

A group of rebels fight against the evil Galactic Empire.

Stranger Things – Season One

A group of friends in the 1980s investigate the mysterious disappearance of their friend, Will Byers, and uncover a dark government conspiracy.

Westworld

A futuristic theme park where visitors can interact with lifelike androids.

Adding the subgrid

All the magic happens in this demo when we apply subgrid to the grid-template-rows property on the HTML article element.

article {
  display: grid;
  grid-row: span 3;
  grid-template-rows: subgrid;
}

The layout is deliberately a bit rough

I’ve left the descriptive text ‘as-is’ to more easily highlight that each row within the column is now properly spaced. In reality I would probably use the -webkit-line-clamp hack to truncate at 2 or 3 lines.

I also added word-break: break-all to the header and descriptive text to make it look a little better as the real estate is a little limited and my choice of font-size a tad large.

Download sample code

I’ve assembled all the parts so that you can play with them. The standard – software is provided “as is”, without warranty of any kind – applies here.

Download File
Zip Files Info
index.html
2.6KB
style.css
2.7KB
readme.txt
460 bytes
westworld.jpg
69.5KB
star-wars.jpg
101.8KB
stargate-sg-1.jpg
107KB
stranger-things.jpg
103.4KB
battlestar-galactica.jpg
71.4KB
star-trek.jpg
129.5KB
File: barrd-align-with-subgrid.zip
Details: 9 files @ 589.8KB

That’s all folks

This has been a missing piece of the puzzle for a long time and whilst the Chromium-based browsers are lagging behind there does appear to be a light at the end of the tunnel. Hopefully soon all the major players will be up to speed and this quest will be at an end.

// End of Project

More Information

Dave Barr

Bristol based Scottish Expat who has 20+ years experience of Web Development and is continually on the look out to improve his skill sets. Learning new and innovative solutions for current requirements in the world of IT, WebDev and eCommerce.

About Dave Barr

Image for Dave Barr
Bristol based Scottish Expat who has 20+ years experience of Web Development and is continually on the look out to improve his skill sets. Learning new and innovative solutions for current requirements in the world of IT, WebDev and eCommerce.

Read more about Dave

Click to Copy