Thursday, 1 February 2018

Aspect Ratios with SVG

I quite like this little trick from Noam Rosenthal:

<style>
.aspectRatioSizer {
  display: grid;
}
.aspectRatioSizer > * {
  grid-area: 1 / 1 / 2 / 2;
}
</style>

<div class="aspectRatioSizer">
  <svg viewBox="0 0 7 2"></svg>
  <div>
    Content goes here
  </div>
</div>

Two things going on there:

  1. As soon as you give a <svg> a viewBox, it goes full-width, but only as tall as the implied aspect ratio in the viewBox value. The viewBox value is essentially "top, left, width, height" for the coordinate system interally to the SVG, but it has the side-effect of sizing the element itself when it has no height of its own. That's what is used to "push" the parent element into an apsect ratio as well. The parent will still stretch if it has to (e.g. more content than fits), which is good.
  2. CSS Grid is used to place both elements on top of each other, and the source order keeps the content on top.

Direct Link to ArticlePermalink


Aspect Ratios with SVG is a post from CSS-Tricks



from CSS-Tricks http://ift.tt/2DAoUlC
via IFTTT

No comments:

Post a Comment

Passkeys: What the Heck and Why?

These things called  passkeys  sure are making the rounds these days. They were a main attraction at  W3C TPAC 2022 , gained support in  Saf...