css - how to stretch and auto-resize background image

前端 未结 6 1807
再見小時候
再見小時候 2020-12-17 21:24

I\'m trying to get my background image to stretch across the entire page, but so far i have this:

\"enter

相关标签:
6条回答
  • 2020-12-17 21:48

    Have you tried using

    background-size: 10px 10px
    

    Change 10 to the width and height of the body

    You can also do

    background-size: 100%
    
    0 讨论(0)
  • 2020-12-17 21:57

    Use background-size: cover. Note its browser support.

    MDN documentation.

    0 讨论(0)
  • 2020-12-17 21:59

    You need to make this an <img> tag, with a low z-index, with position: fixed and then for the <img> tag, use height: 100%; width: auto or the opposite.

    This method is cross-compatible for all browsers.

    0 讨论(0)
  • 2020-12-17 22:04

    You can use:

    background-size: cover;
    

    However please bear in mind which browsers this supports. For example, this won't work in IE8 and below.

    0 讨论(0)
  • 2020-12-17 22:04

    Background size will do the trick:

        body { 
         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;
       }
    

    Check this out for more info: http://css-tricks.com/perfect-full-page-background-image/

    0 讨论(0)
  • 2020-12-17 22:07

    Another option would also include:

    body{
       background-image:url("mybackground.jpg")
       background-size:contain;
       background-repeat:no-repeat;
    }
    
    0 讨论(0)
提交回复
热议问题