Stretch and scale CSS background

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

    Try the article background-size. If you use all of the following, it will work in most browsers except Internet Explorer.

    .foo {
        background-image: url(bg-image.png);
        -moz-background-size: 100% 100%;
        -o-background-size: 100% 100%;
        -webkit-background-size: 100% 100%; 
        background-size: 100% 100%;
    } 
    
    0 讨论(0)
  • 2020-11-22 02:55

    Use the border-image : yourimage property to set your image and scale it upto the entire border of your screen or window .

    0 讨论(0)
  • 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')";
    
    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-11-22 02:58

    This is what I've made of it. In the stretch class, I simply changed the height to auto. This way your background picture has always got the same size as the width of the screen and the height will allways have the right size.

    #background {
        width: 100%;
        height: 100%;
        position: absolute;
        margin-left: 0px;
        margin-top: 0px;
        z-index: 0;
    }
    
    .stretch {
        width:100%;
        height:auto;
    }
    
    0 讨论(0)
  • 2020-11-22 03:00

    An additional tip for SolidSmile's cheat is to scale (the proportionate re-sizing) by setting a width and using auto for height.

    Ex:

    #background {
        width: 500px;
        height: auto;
        position: absolute; 
        left: 0px; 
        top: 0px; 
        z-index: 0;
    }
    
    0 讨论(0)
提交回复
热议问题