Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parent div's width larger than children's width in Firefox

I have a strange issue in Firefox.

I have a div with height defined in constant px value, and there is an img element within that. I have no problem with this setup in chrome, but in firefox parent div's width turns out to be larger than the img in it.

This is the html structure:

<div class="wrapper">
    <div class="imageHolder">
        <img src='dasource'>
    </div>
</div>

And this is the css:

.wrapper {
    width: 900px;
}

.imageHolder {
    height: 400px;
    width: auto;
    background-color: green;
    float: left;
    max-width: 50%;
    overflow: hidden;
}

.imageHolder img {
    height: 100%;
}

http://jsfiddle.net/MXudn/6

As explained in this fiddle, in firefox, the parent div turns out to be larger than the image in it.

Any ideas why this is the case?

like image 450
Ozgur Akcali Avatar asked Jun 27 '13 16:06

Ozgur Akcali


People also ask

How do you make a child div element wider than the parent div?

In this case, we set the child element's width to be 100% of the viewport width by using a percentage viewport unit (vw), then, we move it to the left side (by the distance of the viewport's half, minus 50% of the width of the parent element) with the left property.

How do you make a div full width of a parent?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.


1 Answers

This does look like a bug in Firefox to me. For some reason overflow: hidden is causing the parent div to use the width of the unscaled image rather than post-scaling.

http://jsfiddle.net/MXudn/8

<div class="imageHolder">
    <img src='http://placehold.it/650x650' />
<div>
.imageHolder {
    height: 400px;
    background-color: green;
    float: left;
    overflow: hidden;
}

.imageHolder img {
    height: 100%;
}

In this stripped down example, you can clearly see the issue. The image is originally 650px wide, rescaled based on height, it becomes 400px wide. The parent however, remains 650px wide.

If you do not need the overflow: hidden simply removing that fixes the problem.

http://jsfiddle.net/MXudn/12/

EDIT: Firefox bugzilla ticket for this issue.

like image 151
James Montagne Avatar answered Oct 05 '22 14:10

James Montagne