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

HtmlCssBackground Image

Html Problem Overview


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

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

For some reason, when I do this, the width of myImage stretches across the entire screen but the height only stretches until the height of everything else on the page. So if I put a bunch of

<br>

in my html page, then the height will increase. If my HTML page consists only of a

<div id='header'>
    <br>
</div>

then the height of the background image would just be the height of one

<br>

How do I make the height of my background image 100% of the screen which the user is using to view the webpage?

Html Solutions


Solution 1 - Html

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/

Solution 2 - Html

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/

Solution 3 - Html

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.

Solution 4 - Html

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSilentDevView Question on Stackoverflow
Solution 1 - HtmlSomeKittensView Answer on Stackoverflow
Solution 2 - HtmlDerek StoryView Answer on Stackoverflow
Solution 3 - HtmlKjeld SchmidtView Answer on Stackoverflow
Solution 4 - HtmlMostafa NorzadeView Answer on Stackoverflow