0

I basically want to make a full viewport background image.

This is my code:

<html>
    <body>
        <div class="container">
        <img src="loginBg.png" height="100%" width="100%">  
    </div>
</html>

And stylesheet goes like:

.container{
    position:absolute;
    top:0px;
    bottom:0px;
    right:0px;
    left:0px;
}

The problem with this approach is, the image is full screen but there is some space below it, because of this space the page is scrollable. I checked it in firefox ubuntu, the white space is not there.

Thanks in advance

emmanuel
  • 9,607
  • 10
  • 25
  • 38
Teja Reddy
  • 561
  • 1
  • 4
  • 12

3 Answers3

2

Real solution - CSS background images

Use a CSS background image on the body in conjunction with no-repeat and background-size.

2 Examples

With background-size: cover to maintain height and width ratio

body {
    background: url(http://www.placehold.it/200) no-repeat;
    background-size: cover;
}

With background-size: 100% 100% to stretch

Note: html,body { height: 100% } allows the percentage background size.

html,body {
  height: 100%;
}

body {
  background: #F00 url(http://www.placehold.it/200) no-repeat;
  background-size: 100% 100%;
}

Read more:

Community
  • 1
  • 1
misterManSam
  • 24,303
  • 11
  • 69
  • 89
1

Js Fiddle

use display:block for the image issue will be fixed

.container{
    position:absolute;
    top:0px;
    bottom:0px;
    right:0px;
    left:0px;
}
img{
    display:block;
}
Vitorino fernandes
  • 15,794
  • 3
  • 20
  • 39
1

You could use a background image on the body tag, which makes a lot of sense.

The problem that you noticed is due to the image being an inline element, which adds some space below the image and triggers an overflow condition.

You can fix by adding:

.container img {
    display: block;
}

You could also use vertical-align: top to remove the extra white space or apply overflow: hidden to hide the overflow, all workable options.

Marc Audet
  • 46,011
  • 11
  • 63
  • 83