CSS: stretching background image to 100% width and height of screen?

后端 未结 4 1444
深忆病人
深忆病人 2020-11-29 03:19

I have an image called myImage.jpg. This is my CSS:

body {
    background-image:url(\"../images/myImage.jpg\");
    background-repeat: no-repeat;
    backgro         


        
相关标签:
4条回答
  • 2020-11-29 03:38

    The VH unit can be used to fill the background of the viewport, aka the browser window.

    (height:100vh;)

    html{
        height:100%;
        }
    .body {
         background: url(image.jpg) no-repeat center top; 
         background-size: cover; 
         height:100vh;     
    }
    
    0 讨论(0)
  • 2020-11-29 03:43

    You need to set the height of html to 100%

    body {
        background-image:url("../images/myImage.jpg");
        background-repeat: no-repeat;
        background-size: 100% 100%;
    }
    html {
        height: 100%
    }
    

    http://jsfiddle.net/8XUjP/

    0 讨论(0)
  • 2020-11-29 03:43

    I would recommend background-size: cover; if you don't want your background to lose its proportions: JS Fiddle

    html { 
      background: url(image/path) no-repeat center center fixed; 
      -webkit-background-size: cover;
      -moz-background-size: cover;
      -o-background-size: cover;
      background-size: cover;
    }
    

    Source: http://css-tricks.com/perfect-full-page-background-image/

    0 讨论(0)
  • 2020-11-29 03:49
    html, body {
        min-height: 100%;
    }
    

    Will do the trick.

    By default, even html and body are only as big as the content they hold, but never more than the width/height of the windows. This can often lead to quite strange results.

    You might also want to read http://css-tricks.com/perfect-full-page-background-image/

    There are some great ways do achieve a very good and scalable full background image.

    0 讨论(0)
提交回复
热议问题