Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With CSS, how do I make an image span the full width of the page as a background image?

Tags:

html

css

Say, like in this example here: http://www.electrictoolbox.com/examples/wide-background-image.html

When I do it, I end up getting white borders around the image no matter what I do. What am I doing wrong?

like image 481
Doug Smith Avatar asked Aug 22 '12 23:08

Doug Smith


People also ask

How do I make an image cover the whole page width in CSS?

Magic of 'Background-Size' Property The magic happens with the background-size property: background-size: cover; cover tells the browser to make sure the image always covers the entire container, in this case html .

How do I make my background image fit perfectly in 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.

How do you make a picture fit the page?

If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How would you change the size of the background image so it fits the entire page?

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.


2 Answers

If you're hoping to use background-image: url(...);, I don't think you can. However, if you want to play with layering, you can do something like this:

<img class="bg" src="..." />

And then some CSS:

.bg
{
  width: 100%;
  z-index: 0;
}

You can now layer content above the stretched image by playing with z-indexes and such. One quick note, the image can't be contained in any other elements for the width: 100%; to apply to the whole page.

Here's a quick demo if you can't rely on background-size: http://jsfiddle.net/bB3Uc/

like image 182
sellmeadog Avatar answered Sep 18 '22 17:09

sellmeadog


Background images, ideally, are always done with CSS. All other images are done with html. This will span the whole background of your site.

body {
  background: url('../images/cat.ong');
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
}
like image 25
AliInvestor Avatar answered Sep 17 '22 17:09

AliInvestor