Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use max-width and height for a background image?

Tags:

css

image

fluid

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%; } 
like image 776
technopeasant Avatar asked Jun 10 '11 00:06

technopeasant


People also ask

How can I set background height and width image?

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 ...

Can I scale background image?

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.

How do I limit the size of a background image in HTML?

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.

What should be the size of background image?

We recommend a size of 1600 pixels wide by 900 pixels tall so your background can look great on all devices.


2 Answers

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

like image 82
Dan Avatar answered Sep 21 '22 10:09

Dan


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

like image 45
thirtydot Avatar answered Sep 19 '22 10:09

thirtydot