Question

How can I get an image to stretch the height of a DIV class?

Currently it looks like this:

However, I would like the DIV to be stretched so the image fits properly, but I do not want to resize the image.

Here is the CSS for the DIV (the grey box):

.product1 {
    width: 100%;
    padding: 5px;
    margin: 0px 0px 15px -5px;
    background: #ADA19A;
    color: #000000;
    min-height: 100px;
}

The CSS being applied on the image:

.product{
    display: inline;
    float: left;
}

So, how can I fix this?

Was it helpful?

Solution

Add overflow:auto; to .product1

OTHER TIPS

In the markup after the image, insert something like <div style="clear:left"/>. A bit messy, but it's the easiest way I've found.

And while you're at it, put a bit of margin on that image so the text doesn't butt up against it.

Assuming @John Millikin is correct, the code

.product + * { clear: left; }

would suffice to do the same thing without forcing you to manually adjust the code after the div.

One trick you can use is to set the <div>'s overflow property to hidden. This forces browsers to calculate the physical size of the box, and fixes the weird overlap problem with the floated image. It will save you from adding in any extra HTML markup.

Here's how the class should look:

.product1 {
    width: 100%;
    padding: 5px;
    margin: 0px 0px 15px -5px;
    background: #ADA19A;
    color: #000000;
    min-height: 100px;
    overflow: hidden;
}

This looks like a job for clearfix to me ...

Try the following:

.Strech
{
    background:url(image.jpg);
    background-size:100% 100%;
    background-repeat:no-repeat;

    width:500px;
    height:500px;
}
display:inline  
float:left  

is your problem

Floating makes the parents width not be stretched by the child, try placing the image without the float. If you take the float off, it should give you the desired effect.
Another approach would be to make sure you are clearing your floats at the end of the parent element so that they don't scope creep.

Update: After viewing your link Your height issue as displayed, is because the floats are not being cleared.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top