That simple.
Moving my layout into a fluid territory, working on scalable images. Using the img tag and setting max-width to 100% works perfectly, but i'd rather use a div with the image set as its background.
The issue I'm running into is that the image doesn't scale to the size of the div it's in the background of. Any way to add markup to the background code to set it to 100% width of it's container?
#one { background: url('../img/blahblah.jpg') no-repeat; max-width: 100%; }
There are four different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height), and the multiple background syntax (separated ...
The background-size CSS property lets you resize the background image of an element, overriding the default behavior of tiling the image at its full size by specifying the width and/or height of the image. By doing so, you can scale the image upward or downward as desired.
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.
We recommend a size of 1600 pixels wide by 900 pixels tall so your background can look great on all devices.
As thirtydot said, you can use the CSS3 background-size
syntax:
For example:
-o-background-size:35% auto; -webkit-background-size:35% auto; -moz-background-size:35% auto; background-size:35% auto;
However, as also stated by thirtydot, this does not work in IE6, 7 and 8.
See the following links for more information about background-size
: http://www.w3.org/TR/css3-background/#the-background-size
You can do this with background-size
:
html { background: url(images/bg.jpg) no-repeat center center fixed; background-size: cover; }
There are a lot of values other than cover
that you can set background-size
to, see which one works for you: https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
Spec: https://www.w3.org/TR/css-backgrounds-3/#the-background-size
It works in all modern browsers: http://caniuse.com/#feat=background-img-opts
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