0

Let's say we have a flex container with n children.

If I don't know how height the container is, is it possible to turn one of the children into a keep-aspect-ratio box, as the image shows?

title-logo

Is there any way to achieve with nor javascript nor setting a height for the container?

In order to achieve that proportion, I'm using the following snippets:

[style*="--aspect-ratio"] {
  position: relative;
}

[style*="--aspect-ratio"]::before {
  content: "";
  display: block;
  width: 100%;
  padding-bottom: calc(100% / (var(--aspect-ratio)));
}

[style*="--aspect-ratio"] > * {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
<header class="header">
 <div>
  <!-- ... title -->
 </div>

 <!-- ... perhaps more children -->

 <div class="header__logo" style="--aspect-ratio: 1">
  <!-- ... square box -->
 </div>
</header>

I've tried also to become the square box to an absolute element, but the content does not keep the aspect ratio.

Hugoren Martinako
  • 490
  • 1
  • 7
  • 17

0 Answers0