Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Full Size background image

Tags:

css

Trying to get full size background image with:

html {
    height: 100%;
    background:url('../img/bg.jpg') no-repeat center center fixed;
    background-size: cover;
}

It's showing the background image with correct width but height gets stretched. I tried various options like

html {
background:url('../img/bg.jpg') no-repeat center center fixed;
background-size: 100% auto;
-webkit-background-size: 100% auto;
-moz-background-size: 100% auto;
-o-background-size: 100% auto;

}

removing height: 100%

but none of them worked.

like image 455
Himanshu Yadav Avatar asked Dec 27 '12 18:12

Himanshu Yadav


People also ask

How do I make the background image fit my screen CSS?

Using CSS, you can set the background-size property for the image to fit the screen (viewport). The background-size property has a value of cover . It instructs browsers to automatically scale the width and height of a responsive background image to be the same or bigger than the viewport.

What is CSS background-size?

The background-size CSS property sets the size of the element's background image. The image can be left to its natural size, stretched, or constrained to fit the available space.


2 Answers

Your screen is obviously a different shape to your image. This is not uncommon, and you should always cater to this case even if your screen is the same shape (aspect ratio), because other people's screens will not always be. To deal with this, you have only one of three options:

  • You can choose to completely cover the screen with your image, but not distort the image. In this case, you will need to cope with the fact that edges of your image will be cut off. For this case, use: background-size: cover
  • You can choose to make sure the entire image is visible, and not distorted. In this case, you'll need to cop with the fact that some of the background will not be covered by your image. The best way to deal with this is to give the page a solid background, and design your image to fade into the solid (although this is not always possible). For this case, use: background-size: contain
  • You can choose to cover the entire screen with your background, and distort it to fit. For this case, use: background-size: 100% 100%
like image 55
Gareth Cornish Avatar answered Sep 22 '22 08:09

Gareth Cornish


try with contain instead of cover and then center it:

background-size: contain;
background-position: center;
like image 20
vGichar Avatar answered Sep 24 '22 08:09

vGichar