DEV Community

Discussion on: Why we need CSS subgrid

Collapse
 
uglyeoin profile image
uglyeoin

I've always used Flexbox for the subgrid. Would that solve your problem?

Collapse
 
kenbellows profile image
Ken Bellows

No, the problem is that you can't use Flexbox to align the children of two different parent elements, which is what subgrids allow you to do

Collapse
 
uglyeoin profile image
uglyeoin

Oh I see, you can't do it on any sort of equal widths or percentage widths or anything, it has to flex but be the same. Got ya. An interesting concept. They should invent height = width whilst they are doing it :D

Thread Thread
 
striptogether profile image
StripTogether

If you plan from the very beginning for a layout without pixel-based wrappers, you can kind of achieve this behavior.

Use the viewport width measurement: vw.
Width: 50vw; Height:50vw;

Thread Thread
 
kenbellows profile image
Ken Bellows • Edited

That works if it's okay to base your container size on the viewport, but I've more often wanted to base the container on its dynamic content, while maintaining a fixed aspect ratio.

There are a few CSS Working Group GitHub issues tracking this feature (#333, #1173), and there's actually a "rough draft" section in the CSS Intrinsic & Extrinsic Sizing Module Level 4 Editor's Draft of a proposed aspect-ratio property to fix this, which I'm pretty excited about.

In the meantime, there is a popular ugly hack that works in certain circumstances (but not all) that relies on the fact that percentages in padding, even top and bottom padding, are always based on the width of an element, so that for example you could make a square with:

.square {
  width: /* whatever */;
  height: 0;
  padding-bottom: 100%;
}

...and then a bunch of gross stuff involving absolute positioning and hidden overflows to get the content to sit in the right place... it works, but it's a bit fragile, so use it with caution. CSS Tricks has a great article on it: "Aspect Ratio Boxes"