-
Notifications
You must be signed in to change notification settings - Fork 672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[css-grid] aspect ratio units needed #1173
Comments
I agree. It’s do-able with existing CSS, but it’s a kludge: http://keithjgrant.com/posts/2017/03/aspect-ratios/ |
…as that post points out, overflow issues can get interesting. What do you do when the width is constrained, but the contents need more space than the aspect ratio would allow for? Should an aspect ratio rule be allowed to force the width wider? |
Why would you want to solve this with units? |
There as some discussion of making the svg viewbox apply to css as well. w3c/fxtf-drafts#7 Depending on how it ends up working, you could maybe do things like this: .grid-item { view-box: 0 0 16 9; } Then |
I think this is technically a duplicate of #333? |
After some discussion with @jensimmons, I think the best solution here is to have an |
Here is a useful roundup of current aspect ratio attempts by authors. https://www.bram.us/2017/06/16/aspect-ratios-in-css-are-a-hack/ |
So, I was just thinking about this again and the problem is this: I have two columns of different widths (say 100px and 200px), and then one row indicating it wants an aspect ratio of 1:1. How tall is the row? |
@keithjgrant said
Unfortunately this doesn't work all the time. I am having it fail on me for example when I want my cell to be square or taller, and inside this cell I want a flex which fills the full height, for vertical spacing-out of its children. Firefox is happy but Chrome is not (100% height seems to mean nothing to it in this context), and I've had to fall back to the vertical padding hack, with an absolutely-positioned content container. But this way, the content can't grow the cell if it's too long. |
@fantasai @jensimmons @rachelandrew
I've been wondering about that too. How about saying that the aspect ratio of rows is calculated from the size of implicit columns (and vice versa) if they're sized to a Suggested syntax: allow container {
display: grid;
grid-auto-columns: 1fr;
grid-template-rows: repeat(3, minmax(1.0, auto));
} You get 3 rows and n columns depending on content. All the columns are the same width, and the height of each row is at least the same as the width of the columns, or more if the content wouldn't fit, with each row growing individually. To get all rows to grow together, change to: container {
display: grid;
grid-auto-columns: 1fr;
grid-template-rows: repeat(3, minmax(1.0, 1fr));
} If you want to fix the number of columns instead of rows, but keep the same aspect ratio logic: container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-columns: 1fr;
grid-auto-rows: minmax(1.0, auto); /* or minmax(1.0, auto), depending */
} |
Alternatively, call it out explicitly
container {
display: grid;
grid-template-columns: 100px 50px 300px;
grid-auto-rows: aspect-ratio(1.0 1 / 3); /* 1 * (100px + 50px) = 150px */
} |
Looking back over @jensimmons' comments in #333, I'm going to suggest that we a) have an |
Would that cover minimum aspect ratio as well? meaning that the element maintains the aspect ratio if it is underfilled, but that it does grow rather than overflow if there's too much content? Assuming it solves that, I'm not necessarily opposed to the idea, but I think we should resume exploring w3c/fxtf-drafts#7 before we lock in on that, because it seemed that that approach could give us aspect ratio control (and more) as well.
That sounds reasonable. I wonder how it compares to the |
(Where "ew" stands for element width percentage.) |
I hope this would apply to Flexbox as well (I recently needed always-square cells in Flexbox layout). Should there be a Flexbox label in addition to the Grid label? |
I hope we'll soon get a solution, there even is a website generating a hack: |
yeah, old padding hack is fairly well known, and litters the code with extra divs while taking the content out of the page flow with a position absolute, very fair from a good solution. |
Some versions of the hack don't require a wrapper element But they're all hacks. Looking forward to having a nice native solution ... |
If there is going to be an There are min and max aspect ratios for media queries, they should be modeled after those values. Without min and max versions it becomes difficult to manage text that doesn't fit inside the normal aspect ratio. |
No need for more two properties, the min and max could be part of the value Also perhaps both the aspect ratio and the min & max could be part of the value (as opposed to a new property). Also see all "minmax" in the above comments. |
I wrote a bit about this on Twitter, and got a bunch of responses from web developers: https://twitter.com/jensimmons/status/1053004310840266752 There have been some blog posts written by developers, articulating their desires. Here are two: I believe there's more in the Responsive Images Community Group / other Community Group discussions. I plan to read through that stuff to see what other needs might be articulated to make sure we are meeting them. And to look for words to possibly use when bike shedding names. |
Instead of introducing the 'ar' unit which can relate height to width or relate width to height, code would be easier to read if 'w' is used for width and 'h' is used for height. So for example;
|
Like I said in #333 (why are both still open?): @Archibald2 I see developers already struggle with |
To everyone saying that they want The new unit is being locked down to CSS Grid like the
The bigger the can of worms that gets opened the longer it takes for the new CSS to land in browsers. As for those wondering why #333 is still open, they are different issues. #333 is meant to be focused on adding a new It's not one or the other, they are planning on adding both features to CSS. |
From #333 (comment):
Since padding, border, and margin are all attached to either a horizontal or vertical side, I'm wondering if the
|
Although if there was a way to make |
I would like to share another common use case that needs aspect ratio units. |
@timuric Updated: See WICG/intrinsicsize-attribute#16: img {
aspect-ratio: attr(width) / attr(height);
} |
Given that we now have an |
I agree to closing this. |
The main argument I'm seeing in the thread is so that aspect ratio can be applied to CSS Grid templates. I'm pretty sure using I can't really think of a scenario where I couldn't use |
I wonder whether if we get into a position of being able to style grid areas in some way, this may come back as a requirement. At the moment you have to put an item in the track in order to style an area, but in future maybe not. |
The other main scenario is wanting to style things based on the width/height of the element. For example, it would be absolutely amazing if we could write this to create text that modifies in size based on the width of the element: .responsive-text {
font-size: clamp(1rem, 2%w, 5rem);
}
That isn't "aspect-ratio" though so maybe needs a separate ticket, or maybe we just rename this one? |
@Dan503 I agree. The font-size relative to width or height feels like a pretty different ticket. This is somewhat reminiscent of the CSS container queries efforts. This problem does arise a lot in the aspect-ratio context, but not limited to it. |
I know there has been discussion of aspect ratio units in the past.
We need them. Badly.
Especially with CSS Grid.
Why? When? So — there are many usecases where an Author wants to maintain a specific aspect ratio in their Grid cells. They want every cell to be 4x3 or to be a square.
For example in order to make every cell a square, maybe the browser looks at both min-content dimensions (or both results of auto) — and keeps the longer of the two. Then makes the shorter of the two into the same length as the longer.
Or a similar thing to keep 4x3, or keep 16x9.
I have not had a chance to read the proposed specs. Or code up demos. I just know this comes up over and over and over. I just had yet another person ask me how to do this, and all I can say is "use viewport units and a ton of media queries". We need something better.
The text was updated successfully, but these errors were encountered: