Weird div border problems when floating
Question
Could I please get a explanation of why this code produces the result it does? And a way to fix it/work around it, if possible.
I dont want div 'z' and 'q' to go over 'the blue div border' on the right.
Or
I would like div 'x' to be consitant with 'z' and 'q' and also go over the blue right border as well.
<div style='margin: 5px;width: 653px;border: blue 1px solid;float: left;'>
<div style='margin: 0px; margin-bottom: 5px;width: 100%;border: red 1px solid;/*float: left;*/'>z</div>
<div style='overflow: hidden;margin: 0px; margin-bottom: 5px;width: 100%;border: red 0px solid;/*float: left;*/'>
<div style='margin: 0px; margin-bottom: 0px;width: 300px;border: red 1px solid;float: left;'>y</div>
<div style='margin: 0px; margin-bottom: 0px;width: 300px;border: red 1px solid;float: right;'>x</div>
</div>
<div style='margin: 0px; margin-bottom: 5px;width: 100%;border: red 1px solid;/*float: left;*/'>q</div>
Solution
Throught what browser did you produce the screenshot? If it's IE, there's a problem with the box model that causes the border width to be ignored when calculating 100% width.
Either you create an invisible container to size the inner div's or size your inner div to container.width -2.
Also, try removing the width: 100%; from the div's.
OTHER TIPS
The red-border is actually inside the blue border in your image - but I assume you want to increase the margin on the z and q containers...
I've taken the liberty of enclosing the attributes in double-quotes and correcting the style rules that were re-declared (margin and margin-bottom) - but apologies for the line-formatting - I couldn't seem to get it to all stay inside the code block on this forum until I took out the line breaks:
<div style="margin: 5px;width: 653px;border: blue 1px solid;float: left;"><div style="margin: 5px;width: 100%;border: red 1px solid;">z</div><div style="overflow: hidden;margin: 5px;width: 100%;border: red 0px solid;"><div style="margin: 0px;width: 300px;border: red 1px solid;float: left;">y</div><div style="margin: 0px;width: 300px;border: red 1px solid;float: right;">x</div></div><div style="margin: 5px;width: 100%;border: red 1px solid;">q</div>
For this scenario may be your design is working but can break very easily as you have not cleared and handled floated elements.
You can refer to example I have created.I have created a working fiddle for such kind of problems.
http://jsfiddle.net/mayankipsa/e7snvdag/
.floatLeft { float: left;}
.floatRight { float: right;}
.clearBOTH { clear: both; }
.redBorder{border:1px solid red;}
.blueBorder{border:1px solid blue;}
.x,.y{width:49%;margin:1px; }
.z{margin:1px;}
.q{margin:1px;}
<div style='margin: 5px;width: 653px;border: blue 1px solid;float: left;'>
<div style='margin: 0px; margin-bottom: 5px;width: 100%;border: red 1px solid;/*float: left;*/'>z</div>
<div style='overflow: hidden;margin: 0px; margin-bottom: 5px;width: 100%;border: red 0px solid;/*float: left;*/'>
<div style='margin: 0px; margin-bottom: 0px;width: 300px;border: red 1px solid;float: left;'>y</div>
<div style='margin: 0px; margin-bottom: 0px;width: 300px;border: red 1px solid;float: right;'>x</div>
</div>
<div style='margin: 0px; margin-bottom: 5px;width: 100%;border: red 1px solid;/*float: left;*/'>q</div>
</div>
<div class="clearBOTH"></div>
<div class="blueBorder" style="margin-top:50px;">
<div class="z redBorder">z</div>
<div class="redBorder">
<div class="y floatLeft redBorder">y</div>
<div class="x floatRight redBorder">x</div>
<div class="clearBOTH"></div>
</div>
<div class="q redBorder">q</div>
</div>