Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS body background image fixed to full screen even when zooming in/out

Tags:

css

I am trying to achieve something like this with CSS:

I'd like to keep the body background image fixed on fullscreen, this is sort of done by the following code:

body {     background: url(../img/beach.jpg) no-repeat fixed 100% 100%; } 

Now I can verify the window is indeed filled up with that image, at least this works on my Firefox 3.6

However, it screwed up when I tried to zoom in/out (ctrl+-/+), the image just is stretched/shrinked as the page zooms.

Is there a better way of doing this purely with CSS? I didn't find a good property for the background-image.

Or should I start thinking about jQuery to manipulate the width and height on the fly? They were both set to 100% so I reckon that should work "as always" :(

Thanks for any suggestion in advance!

like image 953
Michael Mao Avatar asked Aug 29 '10 11:08

Michael Mao


People also ask

How do I stretch a background image in CSS to fit the screen?

When you work with background images, you may want an image to stretch to fit the page despite the wide range of devices and screen sizes. The best way to stretch an image to fit the background of an element is to use the CSS3 property, for background-size, and set it equal to cover.

How do I make the background image resize automatically in CSS?

Use background-size property to cover the entire viewport The CSS background-size property can have the value of cover . The cover value tells the browser to automatically and proportionally scale the background image's width and height so that they are always equal to, or greater than, the viewport's width/height.

How do I make my background image full screen responsive?

A simple way to create a responsive full-screen background image is to set a background image that covers the entire window – body { width: 100vw; min-height: 100vh; background: url("IMAGE"); background-size: cover; } .


2 Answers

there is another technique

use

background-size:cover 

That is it full set of css is

body {      background: url('images/body-bg.jpg') no-repeat center center fixed;     -moz-background-size: cover;     -webkit-background-size: cover;     -o-background-size: cover;     background-size: cover; }  

Latest browsers support the default property.

like image 84
Ranjith Siji Avatar answered Oct 03 '22 04:10

Ranjith Siji


I've used these techniques before and they both work well. If you read the pros/cons of each you can decide which is right for your site.

Alternatively you could use the full size background image jQuery plugin if you want to get away from the bugs in the above.

like image 35
Pat Avatar answered Oct 03 '22 04:10

Pat