cssで背景画像をスマホサイズに合わせて自動でサイズ変更させる

● 背景画像をレスポンシブにしてスマホサイズに合わせて自動でサイズ変更させる

● A. 100%表示の場合

html

<div class="myimg">風景写真。このテキストは非表示になります。</div>

scss

.myimg {
    background: url("#{$img_folder}/xxxxx.jpg") 0 0 no-repeat;
    height: 0;
    padding-top: 75.15%;        // 画像の高さを100%とした時の画像の幅%
    background-size: contain;

    overflow: hidden;
    text-indent: -100%;
    white-space: nowrap;
}

● B. width , height を指定する場合

html

<div class="myimg">風景写真。このテキストは非表示になります。</div>

scss

.myimg {
    width: calc( 50vw - 20px );
    width: calc( (50vw - 20px) * 0.7515 );

    background: url("#{$img_folder}/xxxxx.jpg") 0 0 no-repeat;
    background-size: contain;

    overflow: hidden;
    text-indent: -100%;
    white-space: nowrap;
}

引用: https://goo.gl/AyJGb3

No.1409
01/11 15:58

edit