Stretch and scale CSS background

前端 未结 16 2197
难免孤独
难免孤独 2020-11-22 02:28

Is there a way to get a background in CSS to stretch or scale to fill its container?

16条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 02:57

    For modern browsers, you can accomplish this by using background-size:

    body {
        background-image: url(bg.jpg);
        background-size: cover;
    }
    

    cover means stretching the image either vertically or horizontally so it never tiles/repeats.

    That would work for Safari 3 (or later), Chrome, Opera 10+, Firefox 3.6+, and Internet Explorer 9 (or later).

    For it to work with lower verions of Internet Explorer, try these CSS:

    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.myBackground.jpg', sizingMethod='scale');
    -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='myBackground.jpg', sizingMethod='scale')";
    

提交回复
热议问题