I stumbled upon a difference in layout rendering between Safari and Chrome/Firefox and I don't know which one is "right".
You can check the jsfiddle here
On Firefox/Chrome the layout is as expected, the yellow div is right after the red ones. But on Safari, the yellow div is positioned under the red ones.
After investigating what I did wrong I found out the bug comes from the CSS class E
whose property margin-right
(value: -11px) is bigger than the width
property (value: 10px) for my div.
I think I understand why Safari renders it this way. The width of div of class B
is computed as being the sum of the widths of its children as they have the property float: left;
.
Here it is widthB = widthB2*2 + widthE + marginRightE + widthC
or marginRightE < -widthE
so widthB is not large enough to contain each div next to each other.
So my questions are:
- Am I right in my understanding of what Safari does?
- Why do Chrome and Firefox render differently? Are they just not decreasing the width of the parent div based on a negative
margin-right
? - Would the proper correction to always have a
margin-right
lesser or equal to the width of a div in this case?
Thank you!
HTML:
<div class="A">
<div class="C">
<div class="B">
<div class="B2"></div>
<div class="B2"></div>
<div class="E"></div>
<div class="C">
<div class="D"></div>
</div>
</div>
</div>
</div>
CSS:
.A {
background-color: blue;
height: 200px;
}
.B {
height:100px;
}
.B2 {
background-color: red;
height: 100px;
width: 100px;
float: left;
}
.C {
float: left;
}
.D {
height: 40px;
width: 40px;
float:left;
background-color: yellow;
}
.E {
height: 50px;
width: 10px;
position: relative;
left: -10px;
margin-right: -11px;
background-color: black;
float: left;
}