-
Notifications
You must be signed in to change notification settings - Fork 679
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][css-sizing] Aspect Ratio #333
Comments
Filing also under Grid, because it needs to be handled in a way that also works with grid-template-rows/cols. |
https://lists.w3.org/Archives/Public/www-style/2018Jan/0023.html is a related problem; we could solve it if HTML calculates an |
How about a simple ew relative unit added that is percentage of element width? Then we can just Obviously with an addendum that ew is ignored on the width property. This way, it can apply to all elements as they already work? |
This feature really is needed. It's a common use case to have to ensure a certain aspect ratio for each item, eg in Flexbox or Grid, through all viewport widths / element widths. There are many results for just one hack We need a real solution. @ewanm89 wrote
I think I like it. |
Another aspect ratio ticket: #1173 |
@fantasai wrote:
The Grid label unfortunately has been removed. I hope it will get added back. |
We removed the label because it's not a blocker for Grid 2; it's a Sizing issue. We'll want to think about Grid when designing a solution, but in the meantime it's clogging up our view of what needs fixing for Grid. ^_^ |
While I would agree with the principle, the sizing issue has been something that should have been fixed decades ago. And it is just getting more and more desperate without a fix in sight. |
Having an element width "ew" unit would be so useful ... I already use it via EQCSS. An example from one of my projects:
The nice thing is that if .page has a max-width, the effective ew value will stop growing after that max width. With vw the effective value would continue to grow with the viewport. And it could be a nice way of specifying aspect ratios (as @ewanm89 wrote earlier), eg |
This specific example of Regardless, I'd be more interested in a property or method (#820 (comment)) that comes with constraints on display and box, cleanly interacting with min/max-height/width and values like min/max-content/min()/max()/etc... something to more cleanly accomplish this... http://jsbin.com/yuvaka/9/edit?html,output |
It's code from an actual project, and it works 😀 The more typical element query would look like this, for example:
Yep, that's what this EQCSS-powered code does.
It comes from the .page element, which could be the parent of the image element, but it could also be a far higher ancestor for example. The docs are at https://elementqueries.com/ -> "ew (element width)" "EW Units", Some more instances of "ew" units in use:
Having a property (or set of properties) specifically for aspect-ratio would be great! The "ew" unit is a fundamental part of element queries / container queries, and it could also be used for ensuring any aspect ratio:
... but having one or more properties specifically for aspect-ratio would be even better (and I hope we'll get native element queries including element-width units in any case). |
That wouldn't be how an I think if I'm glad the grid tag was removed. This is a unit that should be usable anywhere like the Imagine how great it would be to be able to write |
It could be spec'd to work just as in EQCSS.
This is a later example from above:
Yes, sure! Perhaps you want to open a new ticket for the ew unit? (there's isn't one yet)
In But even better for that type of use case would be to be able to say "always fit this line of text in this container (by adjusting property foo eg font-size or letter-spacing), no matter what font is used". Related: #2528 |
Why open a new issue? This issue is about adding a way to define an aspect ratio. And the What should be discussed separately is the Sebastian |
Because it's a potentially separate feature, and has many, many use cases beyond being one potential option for ensuring aspect ratios.
Yes it sure can be discussed here! ... as far as it pertains to the use case of specifying aspect ratios. And the ew unit has many other use cases so it would sure be sensible to open a separate ticket in addition to that. This aspect ratio ticket here might well get closed as soon as there's a solution which doesn't involve or require "ew" units - then there'd be no ticket for all the use cases that the ew unit solves (eg specifying font-size based on an some element's with).
You wrote that
can be discussed here as long as it specifies an aspect ratio (and people are working on element queries anyways, see https://github.com/tomhodgins/cq-usecases , so there's no need for a new ticket for that). |
This past week, we wrote a draft at: https://drafts.csswg.org/css-sizing-4/#ratios |
From a quick look: Very cool 👍 Regarding the options: option B "min-height: 1ar" is nicely descriptive, and it requires just one line (as opposed to option A). |
@Dan503 thanks, it may well be that all we need here is an informative note directing readers who think they've found the answer to their video aspect ratio problems to the correct place, i.e. CSS Images. |
This is a use-case that an aspect-ratio unit may be helpful in: |
@jensimmons I don't think it is still common indeed, but here is at least one example with LinkedIn Learning: |
Issue 4 in the spec asks:
[...]
This is an interesting idea for how to solve the same problem that the proposed So I guess first of all – I just want to cc some Second – I have two questions about how the proposed rule may break existing content. ①Specifying ②Currently, in order to achieve fluid sizing without aspect-ratio distortion on an image with <img width="300" height="200" style="width: 100%; height: auto;" />
<!-- without `height: auto` this would be locked to a height of 200px, even as the width was fluid/variable --> I suppose this new default would obviate that: a UA default <img width="300" height="200" style="width: 100%;" /> and get a 3:2 fluid img (that was 3:2 even before the image loaded and an intrinsic size was known). Yay! But... is there any existing web content that relies on or expects the previous behavior? Maybe, uh, spacer gifs? |
One could change the UA selector to be more like Can |
long story short, if |
@loonybear my fault for not really asking a question! I guess my first implicit question, which you answered, was "would there be any weird interactions between |
Maybe it would be weird to use
and (in the presence of the proposed UA default style)
are functionally equivalent – they're just two good ways to solve the same problem. In which case, great! (and sorry for all of the comments). |
Just wanted to make sure folks know of...
Both for commenters to learn from and for those involved then to feel free to chime in. |
aspect-ratio works on all replaced elements whereas intrinsicSize so far only works on HTMLImageElement and HTMLVideoElement. The intention is for it to work well with feature policy 'unsized-media' which deals with problems that cause layout instability (content jumping around on the web page). But if aspect-ratio could cover all the cases we need intrinsicSize for (being able to determine the layout size before downloading the image), then I don't think there's a need for intrinsicSize. So how would aspect-ratio achieve the case of: <div style="width: 75vw">
<img src="my-image.jpg" aspect-ratio="4x3" style="max-width: 100%">
</div> i.e. we want to set the image's width to min(width of container, intrinsic width of image). Is it possible for aspect-ratio to do so before downloading any image data. Another question would be, how does it work with responsive images, how will the naturalWidth and naturalHeight be updated if an aspect-ratio is specified? |
@eeeps So about your backwards-compat concerns in #333 (comment) ...
I'm expecting this to be implemented in C++ rather than CSS, but in the case of CSS percentages would end up invalid here (invalidating the rule) and in C++ we would certainly want to do the same.
Yes. I'm not proposing to change that: the |
The main concern wrt Web compat is someone putting in |
With a huge chance of side-tracking this thread (due to not really knowing where exactly to post this reply) or the risk of proposing something that got proposed before, here goes: Glad to see the addition of Thinking further on this, this solution is somewhat limited: it only solves the aspect ratio problem, and nothing else outside of that. A more broader solution – I think – could be the introduction of a The In code, it would result in something like this: .box {
width: 100%;
height: calc(val(width) * 16 / 9);
} Of course it needs some more tweaking and thought: What if you want percentages (for use within I know, it's a bit side-tracky this reply, but I do see a future for this kind of addition. Don't let this comment hinder the release of |
I think people have circled around permutations of this idea. I think where it starts to break down is combinations like: .box {
width: calc(val(height) * 9 / 16);
height: calc(val(width) * 16 / 9);
} However, IIRC, custom properties have some sort of algorithm to break recursion, so that this is valid: .box {
--width: calc(var(--height) * 9 / 16);
--height: calc(var(--width) * 16 / 9);
} I can't recall the algorithm, but I think it has something to do with figuring out the computed value first, so maybe this would work as well? |
@bramus In short, though, I think your point is very valid. People have wanted an "element-relative unit" or some way to calculate the allocated height / width of an element for some time. I discussed it in this proposal: WICG/container-queries#12 However, in this comment, @dbaron addresses the performance penalty of "calculated units" (I proposed
So, in short, the value of |
Forgot to post an update: CSSWG adopted an |
@fantasai in Example 6 of the aspect-ratio minimum section, the width of the child boxes adds up to 200px but the spec says it adds up to only 150px. Also the boxes are |
@Dan503 That example is correct: the "min-content" width is the narrowest width it can take without overflowing, so 150px. |
(stumbled upon this issue while searching for something else) This issue can be closed, no? |
The specific WICG Aspect Ratio Incubator Project was closed and there's a message on its README.md file redirecting the discussion to this issue here. I wonder why this was done, since the Pixel Aspect Ratio isn't restricted to CSS. I can't even post a new issue there. I'm trying like to bring awareness to the problem that the Pixel Aspect Ratio (PAR) is still not rendered correctly by any of the current browsers I tested. This is a metadata present inside JPG, PNG and GIF files. A lot of content was created in the 80's and 90's that had non-square pixels. For example, many legacy pixel art based on rectangular pixels (*1) are being destroyed, as the use case is still present and the users are doing workarounds by artificially stretching the images via resize to have the desired aspect ratio. Once the image is stretched like this, it's nearly impossible to restore it without loss. The same issue affects all images (stills and video) captured from SDTV. NTSC had an effective resolution of 720x480, which means that it's pixels aren't square. Just like pixel art, users have no alternative other than artificially stretch the image to adjust it for square pixels, creating artifacts that will be impossible to cleanup later. Inspired by the HTML AcidTests, I created the Pixel Aspect Ratio Acid Test, that can be seen in the link below. It will check if the browser renders two sets of images correctly. http://frs.badcoffee.info/PAR_AcidTest/ *1: Read C64, CGA, EGA, VGA (320x200), SNES, MSX, Amiga, TurboGrafx-16, NEC PC-88 and nearly the majority of devices from the 80s, when square pixels were still an expensive luxury. |
Grid & Flexbox make the aspect ratio problem more urgent, since they're so much better at flexible sizing than previous layout models...
Jen Simmons wrote:
The text was updated successfully, but these errors were encountered: