Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background-size: cover not scaling image in div

I have four equally sized div's set up like this:

<div id="top-left"></div>
<div id="top-right"></div>
<div id="bottom-left"></div>
<div id="bottom-right"></div>

Each is 50% of the page's width and positioned absolutely. Like this, for example:

#top-right {
position: absolute;
top: 0px;
left: 50%;
width: 50%;
height: 50%;
}

The issue is when I try to scale a (large) background image via the CSS3 cover. This is the background image css I have so far:

background: #000 url('DSC01992.jpg') center center fixed no-repeat;
background-size: cover;
-moz-background-size: cover;
-o-(etc.)

Here is a live example: http://jsfiddle.net/TqQv7/

If you open the placeholder image here: http://placekitten.com/2000/1000 you will see that the image is not being scaled correctly.

Am I missing something?

like image 313
Connor Avatar asked Aug 27 '12 17:08

Connor


People also ask

How do I make the background of an image fit in a div?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.

Why does my background image not cover the whole page?

You need background-size:cover if you want to cover the whole area. The value 'contain' will just ensure that it all fits but will be smaller than the viewport unless by accident it happens to be the same aspect ratio.

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

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.

Why background image is not showing in div?

I recommend moving your css from the inline scope. Assuming that your . png file actually exists, try setting the background size and repeat tags. If that doesn't work, try checking in your browser's developer tools for the response codes and making sure that the url is correct.


1 Answers

Removing the background-position and background-attachment portions of your short-hand background declaration will give you your desired results.

Change:

background: #000 url('http://placekitten.com/2000/1000') center center fixed no-repeat;

To:

background: #000 url('http://placekitten.com/2000/1000') no-repeat;

Here is a demo: http://jsfiddle.net/TqQv7/1/

Using background-position and background-attachment along with background-size : cover is anti-intuitive, you're telling the browser to do two different things there, and it seems that modern browsers still default to the old method rather than the new.

For more information about caveats regarding background-size check-out the MDN docs: https://developer.mozilla.org/en-US/docs/CSS/background-size

like image 98
Jasper Avatar answered Sep 20 '22 07:09

Jasper