title: Div that fits into parent and maintains an aspect ratio
body: <p>I've got a <code>div</code> element that maintains an aspect ratio: it calculates its height based on its width (using the padding trick). What I'd like to do is to put this <code>div</code> into another one by fitting the maximum space available, vertically and horizontally, no crop. I think the closest thing to what I want is the <code>object-fit: contain</code> - which is <code>img</code> only.</p>
  
  <p>I want the div to cover the max height and width possible while maintaining the aspect ratio. No vertical or horizontal crop.</p>
  
  <p>Is it even possible with CSS only? If so, how?</p>
  
  <p>Update:
  A good <a href="https://www.bram.us/2017/06/16/aspect-ratios-in-css-are-a-hack/" rel="noreferrer">article</a> where things are at the moment.</p>
  
  <p>code (Can be any other solution, doesn't have to be built on this snippet):</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>html,
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
  }
  
  .container {
    position: relative;
    width: 100%;
  }
  
  .container:before {
    content: "";
    display: block;
    width: 50%;
    padding-top: 50%;
  }
  
  .embed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: red;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="container"&gt;
    &lt;div class="embed"&gt;
      this should accommodate all the available space and maintain aspect ratio, no crop when width is too wide
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="https://i.stack.imgur.com/rs95z.png" rel="noreferrer"><img src="https://i.stack.imgur.com/rs95z.png" alt="enter image description here"></a></p>