Stretch and scale CSS background

前端 未结 16 2025
难免孤独
难免孤独 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

    .style1 {
            background: url(images/bg.jpg) no-repeat center center fixed;
            -webkit-background-size: cover;
            -moz-background-size: cover;
            -o-background-size: cover;
            background-size: cover;
    }
    

    Works in:

    • Safari 3+
    • Chrome Whatever+
    • IE 9+
    • Opera 10+ (Opera 9.5 supported background-size but not the keywords)
    • Firefox 3.6+ (Firefox 4 supports non-vendor prefixed version)

    In addition you can try this for an ie solution

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

    Credit to this article by Chris Coyier http://css-tricks.com/perfect-full-page-background-image/

提交回复
热议问题