Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: How to set container size equal to background image size

Tags:

css

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:

  1. Change image in Photoshop (likely end up with a slightly different image dimension)
  2. Remember that new dimension
  3. Go into CSS file looking for that particular element which uses that image as bg
  4. Change width and height of the element (if i still remember them correctly..)
like image 277
Philip007 Avatar asked Apr 12 '13 01:04

Philip007


People also ask

How do I make an image the same size as the CSS container?

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.

How can I get div height to auto adjust to background-size?

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.

How do I make my background image fit perfectly in CSS?

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.

How do I make the background image resize automatically in CSS?

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.


3 Answers

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;
}
like image 135
thgaskell Avatar answered Sep 28 '22 01:09

thgaskell


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');
}
like image 32
cimmanon Avatar answered Sep 28 '22 03:09

cimmanon


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);
})();
like image 31
jpmorin Avatar answered Sep 28 '22 02:09

jpmorin