Skip to content

Instantly share code, notes, and snippets.

@renoinn
Last active August 11, 2020 13:23
Show Gist options
  • Select an option

  • Save renoinn/e8ee874049a395ae6250333aa63ca6d3 to your computer and use it in GitHub Desktop.

Select an option

Save renoinn/e8ee874049a395ae6250333aa63ca6d3 to your computer and use it in GitHub Desktop.
アスペクト比を合わせるボックスのサンプル。

CodePen: https://codepen.io/renoinn/pen/abNvJZr

参考記事: https://css-tricks.com/aspect-ratio-boxes/

paddingにパーセント指定する場合、widthを基準にするので、それを利用することでアスペクト比を保ったboxを作る。

<html>
  <style>
    .aspect-ratio-box {
      width: 50%;
      height: auto;
      padding-top: 50%;
      background-color: red;
      position: relative;
    }
    .inner-box {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }
  </style>
  <body>
    <div class="aspect-ratio-box">
      <div class="inner-box">
        testtest
      </div>
    </div>
  </body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment