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?
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.
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.
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.
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.
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
andbackground-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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With