Chat Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining the forum??
May 03, 2026 - @461.79 (what is this?)
Activity rating: Four Stars Posts & Arts: 75/1k.beats ~ Boop! The forum will close in 539.beats! Random | Recent Posts | Guild Recents
News: inconvenience is counterculture :eyes: Guild Events: Spring Themed Projects

+  MelonLand Forum
|-+  Life & The Web
| |-+  ✁ ∙ Web Crafting
| | |-+  CSS Subgrids are rarely useful... but sometimes they're amazing!


« previous next »
Pages: [1] Print
Author Topic: CSS Subgrids are rarely useful... but sometimes they're amazing!  (Read 66 times)
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
« on: April 30, 2026 @437.50 »

I just want to shout the praises of CSS Subgrids, because they're amazing. They let you use HTML code that represents what things are (e.g. lists), but then still lay things out in any kind of "tabular" way you like.

Here's a recent example. I threw together a website recently that included a schedule of the films that'll be shown at a film night I'm hosting. I wrote the schedule a bit like this:


Code
<ol id="schedule">
  <li>
    <span>Prerolls</span>
  </li>
  <li>
    <time>7:40pm</time>
    <span>Intro from Dan</span>
  </li>
  <li>
    <time>7:45pm</time>
    <span class="film-title">Some Short Film</span>
  </li>
  <li>
    <time>7:48pm</time>
    <span>Intermission</span>
  </li>
  <li>
    <time>8pm</time>
    <span class="film-title">Feature Film</span>
  </li>
  <li>
    <time>9:55pm</time>
    <span>Closedown</span>
  </li>
</ol>

A ordered list is a great semantic choice for this kind of content: it is, after all, a list of things whose order matters! I'm sure you'll agree that the HTML code is very readable (which does my future-self a favour when I come back to this code in five years time or something!).

But the default styling... doesn't look very good:


https://danq.me/wp-content/uploads/2026/04/Screenshot-2026-04-30-at-09-46-59-CodePen-Create-a-New-Pen-https-__codepen.io_.png

A "grid" would be a great way to lay out the list like a timetable, with times running down the side and the corresponding items alongside them, right? But if we apply a grid to each item in the list then it's hard to control the widths of the "columns" all-together: what I'd want is that if one time gets wider, then all times get wider to compensate.

So what we need to do is to apply a grid to the list itself. And then inherit that as a subgrid within each item. Like this:


Code
#schedule {
  /* Stop it looking "like a numbered list": */
  list-style: none;
  padding: 0;
  
  /* Lay it out in a grid: */
  display: grid;
  
  /* Small gap between items; bigger gap between times and titles */
  gap: 0.5ch 1ch;
  
  /* 1st just wide enough, 2nd fills the space: */
  grid-template-columns: min-content 1fr;
  
  li {
    /* Each list item spans both columns and continues the grid: */
    display: grid;
    grid-column: span 2;
    grid-template-columns: subgrid;
    
    /* The <span> always goes in the second column (even if there
     * is no <time>, like for the "prerolls" item):
     */
    span {
      grid-column: 2;
    }
  }
}

Now my timetable's laid-out right, but still from the same simple, readable (and semantic) HTML:

https://danq.me/wp-content/uploads/2026/04/Screenshot-2026-04-30-at-09-59-48-CodePen-Create-a-New-Pen-https-__codepen.io_.png

I just need a little more CSS to make it prettier and I'm all set:

Code
#schedule {
  /* Wide enough to contain the text without wrapping, unless necessary: */
  width: max-content;
  
  /* General appearance: */
  background: linear-gradient(#9b5f00, #672c00);
  color: #fbfbfb;
  font-weight: 300;
  padding: 2ch 1ch;
  transform: rotateZ(-3deg);
  box-shadow: 4px 4px 12px black;
  
  /* Line between items (i.e. above each except the first!) */
  li:not(:first-child) {
    padding-top: 0.75ch;
    border-top: 2px dotted black;
  }
  
  time {
    /* Small text, center-right aligned: */
    font-size: 65%;
    text-align: right;
    align-self: center;
    
    /* General appearance: */
    font-family: monospace;
    color: #bbb;
  }
}


(I'd meant to make .film-titles italic; whoops; I'll fix that later!)

Now I've got something moderately-pretty from the simplest and most-accessible HTML code imaginable. If I want to replicate this on another page, all I need is a list with the same id="schedule":


https://danq.me/wp-content/uploads/2026/04/Screenshot-2026-04-30-at-10-15-31-CodePen-Create-a-New-Pen-https-__codepen.io_.png

And because all of the layout and style is part of the CSS, leaving the HTML to represent just the "content", I can make alterations on that other page easily. Like if I want to swap which way around the titles and times appear, that's really easy:

Code
#schedule li {
  /* Move <time>s to the right: */
  time {
    grid-column: 2;
    text-align: left;
  }
  
  /* Move <span>s to the left: */
  span {
    /* NOTE use of grid-area rather than grid-column
     * to prevent the <span>s from jumping to column 1
     * of the SECOND row!
     */
    grid-area: 1;
  }
  
  /* Let's make the prerolls (and anything else without
   * a time) centered across both columns instead:
   */
  &:not(:has(time)) span {
    grid-column: span 2;
    text-align: center;
  }
}

https://danq.me/wp-content/uploads/2026/04/Screenshot-2026-04-30-at-10-24-36-CodePen-Create-a-New-Pen-https-__codepen.io_.png

All of this was only a few minutes work (I spent much longer writing this post about it!), and I've gone off-topic a bit now, but the important bit was that CSS subgrids are very rarely useful... but when they are useful, they're amazing!

The other time I've really enjoyed their power was when I needed to lay out a grid of "cards", each of which in turn contained a grid of information. I didn't want to fix the widths of the grids on the cards but wanted them to resize dynamically to whatever screen width was in use, but I still wanted the cards to appear consistent with each other, so subgrids was the winner there too.

Anyway: here's a codepen if you'd like to play with my example.
Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif

Artifact Swap: I met Dan Q on Melonland!PolyamorousBouncy Egg!Joined 2025!Lurby
candycanearter07
Hero Member ⚓︎
*****
View Profile WWWArt


i like slimes
⛺︎ My Room
SpaceHey: Friend Me!
StatusCafe: candycanearter
Itch.io: My Games
RSS: RSS

Guild Memberships:
Artifacts:
it's tbhchansey!Goomy, I Choose You!Suck At Something September - Did It!uh oh! a pigeon got in!Artsy Candy Cane
« Reply #1 on: April 30, 2026 @816.57 »

Grids are cool yea, but I was always a bit confused on the difference between grids and tables?
Logged

new to oldnet be nice
https://status.cafe/users/candycanearter/badge.png https://abslimeware.neocities.org/assets/images/blinkers/penguins.gif

https://abslimeware.neocities.org/assets/images/blinkers/slimebounce.gif https://card.exophase.com/2/0/268504.png?1727352149

https://i.imgur.com/S1cx8ZZ.pnghttps://i.imgur.com/7ntZZGM.pnghttps://i.imgur.com/xKIpW2A.pnghttps://i.imgur.com/YMPbu9R.png

Artifact Swap: charry zardshoeDS Lover (replacement)Ball Creaturecards all the way down
snufkin
Casual Poster
*
View Profile WWW


touching grass
⛺︎ My Room

Guild Memberships:
Artifacts:
Joined 2026!
« Reply #2 on: April 30, 2026 @858.44 »

Dan, this is so cool! Where do you get all this HTML/CSS knowledge?? Can't wait to try this out sometime. Once I find a use for it, that is  :wizard:
Logged

長い旅行に必要なのは大きなカバンじゃなく、口ずさめる一つの歌さ。

Artifact Swap: seahorseySapphire
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
« Reply #3 on: Today at @353.73 »

Dan, this is so cool! Where do you get all this HTML/CSS knowledge?? Can't wait to try this out sometime. Once I find a use for it, that is  :wizard:

1. Time. I've been doing stuff on the Web since the mid-1990s, which gives me a huge leg-up: a new technology (frames, CSS, JavaScript, SVG, whatever) comes along and I learn it as-it-comes. Somebody starting today has catching-up to do before they get that benefit, sadly.

2. Reverse-engineering.
I see something cool; I try to take it apart and work out how it works. I spend lots of time with my browser's debug tools open on other people's websites.

3. The hotness. When I hear about an exciting new feature, I try to grok it and work out what it would be good for. "What can I make it do?" For me, that means hearing about (or seeing somebody else use) something new-to-me and then poring over MDN and playing with it until I feel like I "get" it. Lately, for example, I've been experimenting with View Transitions (which let you apply CSS animations between pages, when a user navigates: I've learned for example that they don't work at all if your CSS isn't in .css files e.g. if you just put them in a <style> tag!) and I've been playing a lot with Web Components (you might have seen two little projects I shared here - <danq-beige-buttons> and <embed-html>).

Next, I'm thinking I might experiment with an upcoming feature, text-scaling, which lets your site opt-in to what is technically an old web feature: making the "zoom" resize the text but not the layout or images, which only works if you've designed with good responsivity.

I can't help you with time; only time itself can do that! But reverse-engineering what others do (asking e.g. "how did they do that?" and working it out; or asking I suppose!) and playing with the things that are "new" are two great ways to boost your web dev game, in my experience.
Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif

Artifact Swap: I met Dan Q on Melonland!PolyamorousBouncy Egg!Joined 2025!Lurby
Pages: [1] Print 
« previous next »
 

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021 | Privacy Notice | ~ Send Feedback ~ Forum Guide | Rules | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi

MelonLand Nav

@000

Want to Login or Join ?

Minecraft: Online
Join: craft.melonking.net