Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the background image to fit into the whole page without repeating using plain css?

I have an JPG image with size 1024 x 724. My page size is not fixed. My requirement is: If I resize the page then the background image should also resize and fit to the page.

Also I don't want to keep the image related information in my Html page/JSP. I want to do this using plain CSS. I am not using CSS3. Because there is an attribute called background-size which can make the image stretch to the page width and height. Currently only Google Chrome supports this. Any help ?

like image 434
Puru Avatar asked Nov 03 '10 08:11

Puru


People also ask

How do I stop a background image from repeating CSS?

To make a background image not repeat in HTML, specify no-repeat in the background-repeat property or the background shorthand property. The background image is set to 'no-repeat' using the 'background-repeat' property.

How do I fit a background image in CSS perfectly?

We can do this purely through CSS thanks to the background-size property now in CSS3. We'll use the html element (better than body as it's always at least the height of the browser window). We set a fixed and centered background on it, then adjust it's size using background-size set to the cover keyword.

How do I stretch a background image to the whole page?

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.


2 Answers

You can't resize background images with CSS2.

What you can do is have a container that resizes:

<div style='position:absolute;z-index:0;left:0;top:0;width:100%;height:100%'>
  <img src='whatever.jpg' style='width:100%;height:100%' alt='[]' />
</div>

This way, the div will sit behind the page and take up the whole space, while resizing as needed. The img inside will automatically resize to fit the div.

like image 192
Ben Avatar answered Sep 19 '22 13:09

Ben


try something like

background: url(bgimage.jpg) no-repeat;
background-size: 100%;
like image 32
heximal Avatar answered Sep 20 '22 13:09

heximal