1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

アスペクト維持 x トリミング x 横幅:レスポンシブ x 最大幅:元画像サイズ で画像表示

Last updated at Posted at 2019-10-29

画像のアスペクト比を維持しながら、指定の比率でトリミングしたい。
表示サイズはレスポンシブ。ただし最大幅は元画像の横幅での表示の例です。

css
    .img-fluid-trim{
        display: inline-block;
    }
    .img-fluid-trim > div{
        height:0;
        width: auto;
        overflow:hidden;
    }
    .img-fluid-trim-1x1 > div{
        padding-bottom:100%; /* w:h = 1:1 */
    }
    .img-fluid-trim-2x1 > div{
        padding-bottom:50% /* w:h = 2:1 */
    }
    .img-fluid-trim-1x2 > div{
        padding-bottom:150%; /* w:h = 1:2 */
    }
    .img-fluid-trim-16x9 > div{
        padding-bottom:56.25%; /* w:h = 16:9 */
    }

    .img-fluid-trim > div > img{
        width:auto;
        height:auto;
        max-width:100%;
        max-height:initial;
      /*transform: translate(0, -10%); トリミング開始高さを変える場合*/
    }
html
        <div class="img-fluid-trim img-fluid-trim-16x9" >
            <div><img src="/path/to/img.jpg" ></div>
        </div>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?