RSS FeedTwitterMastodonBlueskyShare IconHeart IconGithub IconArrow IconClock IconGUI Challenges IconHome IconNote IconBlog IconCSS IconJS IconHTML IconShows IconOpen Source Software IconSpeaking IconTools IconShuffle IconNext IconPrevious IconCalendar IconCalendar Edit IconNotebook IconObservable Notebooks IconSlash IconGoogle G Icon
Text emphasized alt text example
A cartoon skull with a hotpink hat on.3 min read

3 Unintuitive CSS Layout “Solutions”

css

Here are a few of my least favorite, easy to forget, unintuitive CSS layout "solutions":

.wat {
  min-height: 0;
  min-width: 0;
  flex-shrink: 0;
}

Height 0? Wat? Shrink 0? Wat? Theo thinks he likes it. But…

Then there's minmax(min(), 1fr)? Wat?

.uhhhhhh {
  grid-template-columns: repeat(auto-fill, minmax(min(10rem, 100%), 1fr))
}

Functional programming much?…

Remove intrinsic minimal sizing #

The minimum width of grid and flex children is auto. Setting it explicitly to 0 removes the intrinsic size, unlocking various things.

.example-1 {
  min-height: 0;
  min-width: 0;
}

Study more about it on CSS Tricks by Chris Coyier or from Theo over on YouTube.

Don't squish me #

Often I'll make a flex layout where 1 child should be "pushy" or a dominant space consumer. Works great until a sibling to the pusher goes oval at small viewports, even though a firm height and width are set.

The fix:
Don't squish me, my flex-shrink is 0... which indicates false? which means I don't want to shrink? I am unshrinkable. I have no shrinkability.

.example-2 {
  flex-shrink: 0;
}

So intuitive. Not.

Intrinsically responsive grid columns #

So I don't have this full, triple nested function memorized. I can remember "repeat, auto-fit, minmax" as like the summary of the trick. Which gets me this far:

.example-3 {
  grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr))
}

But that's not the full snippet I end up using in production. It works great until it's a single column of 10rem minimum width boxes. At which point, they dont go smaller, and so they can cause horizontal overflow in a parent container that's smaller than 10rem.

The fix:
add min(10rem, 100%) in place of 10rem.

.example-3 {
  grid-template-columns: repeat(auto-fill, minmax(min(10rem, 100%), 1fr))
}

It's extra to write, but it unlocks the "intrinsically responsive grid layout", which behaves as I was hoping all along.

Study more in this post from Evan Minto:

When the container is smaller than 10rem, the minimum value in our minmax() expression is set to the full width of the container, giving us a nice single-column view with no overflow. When the container is larger than 10rem, the minimum value is set to 10rem, so we get a responsive grid with equal tracks of at least 10rem each.

2 mentions #

41likes
12reposts
  • Jack Iakovenko
  • GENKI
  • Yash Raj
  • Zacky Ma
  • Felix
  • Christian Niklas
  • adrien
  • Andris Klaipins
  • Tyler Sticka
  • Roni Laukkarinen
  • Noah Liebman
  • Jaakko Niemi

Join the conversation on

Great post overall. But I do think flex-shrink: 0; is intuitive, because it is the factor at which it shrinks compared to other items. So the factor of zero means don’t shrink. It makes sense to me—is that weird?
Tobias FedderTobias Fedder
That last one is so robust it almost feels like magic.
James BasooJames Basoo