With CSS, how do I make an image span the full width of the page as a background image?

前端 未结 3 1995
独厮守ぢ
独厮守ぢ 2021-01-12 13:34

Say, like in this example here: http://www.electrictoolbox.com/examples/wide-background-image.html

When I do it, I end up getting white borders around the image no m

3条回答
  •  醉梦人生
    2021-01-12 14:06

    You set the CSS to :

    #elementID {
        background: black url(http://www.electrictoolbox.com/images/rangitoto-3072x200.jpg) center no-repeat;
        height: 200px;
    }
    

    It centers the image, but does not scale it.

    FIDDLE

    In newer browsers you can use the background-size property and do:

    #elementID {
        height: 200px; 
        width: 100%;
        background: black url(http://www.electrictoolbox.com/images/rangitoto-3072x200.jpg) no-repeat;
        background-size: 100% 100%;
    }
    

    FIDDLE

    Other than that, a regular image is one way to do it, but then it's not really a background image.

提交回复
热议问题