2

It's 2021 and aspect-ratio is supported in all modern browsers. Several years ago someone asked a question about how to fill a parent while maintaining an aspect ratio, however the sole answer that mentioned aspect-ratio produces an incorrect result in Firefox.

What is the correct way to expand an element to fit its parent while contrained to its aspect-ratio property? The result I desire is exactly what was shown in that other question:

parent element in three sizes, the child element fills available space as its aspect ratio allows

Here is a non-working starter snippet, see how it breaks when the parent is made wider:

#parent {
  display: grid;
  place-content: center;
  
  width: 100px;
  height: 150px;
  resize: both;
  overflow: auto;
  border: 2px solid black;
}

#child {
  aspect-ratio: 1 / 1;

  background: yellow;
  border: 2px solid orange;
}
<div id="parent">
  <div id="child">I should be an expanding square!</div>
</div>
mrossman
  • 561
  • 4
  • 12
  • 2
    Does this answer your question? [Fill a parent div while maintaining a ratio](https://stackoverflow.com/questions/40249865/fill-a-parent-div-while-maintaining-a-ratio) – mr.Hritik Oct 06 '21 at 01:35
  • use the same code as the other answer. You are claiming it does produce a incorrect result but yours is producing an incorrect result. – Temani Afif Oct 06 '21 at 11:25
  • @mr.lazy No, that's the one I linked in the question. The only answer that uses `aspect-ratio` produces an incorrect result. The other answers are outdated, relying on padding tricks. – mrossman Oct 06 '21 at 19:55
  • @TemaniAfif I know mine produces an incorrect result, that's why I'm asking the question. – mrossman Oct 06 '21 at 19:55
  • Then use the one in the duplicate question that produce a correct result – Temani Afif Oct 06 '21 at 19:56
  • @TemaniAfif the other answers on that post use outdated padding hacks. I am specifically asking about the modern `aspect-ratio` property which is designed for this purpose. – mrossman Oct 06 '21 at 19:57
  • no, there is an answer using the aspect-ratio. Not sure how you missed it. It's the accepted answer, with a bounty and highly upvoted – Temani Afif Oct 06 '21 at 20:05
  • @TemaniAfif please read my question carefully. I am aware there is one answer using `aspect-ratio`. It does not produce the correct result in Firefox. – mrossman Oct 06 '21 at 20:06
  • nothing in your question mention Firefox at all so *carefully* update your question to make it clear. – Temani Afif Oct 06 '21 at 20:26
  • @mrossman That answer is not outdated and the aspect-ratio property is supported in every browser. Maybe you are using Intenet Explorer that's why it's not working. LOL – mr.Hritik Oct 07 '21 at 10:15
  • @mr.lazy I know that aspect ratio is supported in every browser, that's literally the first sentence of my question. I am using Firefox 93 which is a modern browser. The answer that you're referring to [looks like this](https://i.imgur.com/MQkJ0FL.png) in Firefox. That is _not_ the correct result. – mrossman Oct 07 '21 at 15:46
  • @mrossman Give 100% height to **HTML and body**. And if you still facing a problem check this codepen link of my code "https://codepen.io/Hritik25/pen/yLXmmdM" – mr.Hritik Oct 07 '21 at 16:15
  • @mr.lazy I appreciate the suggestion. Unfortunately, when I open your codepen in Firefox 93 [this is what I see](https://i.imgur.com/CBa90fF.png) which is the same incorrect result. The brown region should always appear as a 1:1 square, not a stretched rectangle. – mrossman Oct 08 '21 at 01:06
  • @mrossman I saw output on firefox. – mr.Hritik Oct 08 '21 at 04:50
  • @mrossman post question again or tell me I'll post – mr.Hritik Oct 08 '21 at 18:27

0 Answers0