I know how to stretch background image to fit its container (with background-size
property). But how to achieve the other way around without setting width and height manually?
To better make my point, assume we have a p
element with one line of text and set its background-image to an picture of 800*600px. How to adjust the width and height of p
automatically to 800*600?
I ask the question because I am looking for a better workflow. It's quite annoying to change width and height in CSS every time I change the image size in Photoshop. The workflow is like below:
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.
There is a very nice and cool way to make a background image work like an img element so it adjust its height automatically. You need to know the image width and height ratio. Set the height of the container to 0 and set the padding-top as percentage based upon the image ratio. Save this answer.
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.
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.
Instead of using a background image, you could use a img
element and set the containing div's display to inline-block
. You'd then need to create an inner div to wrap the content and position it absolutely relative to the containing div. Since the img
is the only thing in the flow, the containing div will resize relative to the image.
Pretty much a hack, but I think it would give the effect you are looking for.
http://jsfiddle.net/Km3Fc/
HTML
<div class="wrap">
<img src="yourImg.jpg" />
<div class="content">
<!-- Your content here -->
</div>
</div>
CSS
.wrap {
display: inline-block;
position: relative;
}
.wrap img + .content {
position: absolute;
top: 0;
left: 0;
}
Your only option would be to programatically add the height/width. Compass for Sass has functions that can return the dimensions of the image when the CSS file is compiled: http://compass-style.org/reference/compass/helpers/image-dimensions/
.foo {
height: image-height('my-img.png');
width: image-width('my-img.png');
}
According to the documentation for CSS3 w3schools this should do it:
div {
background-size: contain; /* or cover */
}
EDIT: using javascript, you could load the image from the background-image property and set the size of the container.
(function() {
var img = new Image();
var $mydiv = $('#mydiv');
img.src = $mydiv.css('background-image').slice(4,-1);
$mydiv.width(img.width).height(img.height);
})();
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