In working on a layout, I decided to try combining a float-based layout for the major columns with a table-based layout for the sub-elements. Thus, my html/css markup was along these lines:
HTML:
<div class="column">
<div class="sub-element"></div>
<div class="sub-element"></div>
</div>
<div class="column">
<div class="sub-element"></div>
<div class="sub-element"></div>
</div>
...
CSS:
.column {
float: left;
display: table;
width: 15%;
margin: 2%;
/* ... */
}
.sub-element {
display: table-cell;
/* ... */
}
The specific widths and margins aren't critical. See this jsFiddle for a reference example.
What I saw happening was that each column block, going left to right across the page, had slightly smaller margins than the last. Since no additional markup or CSS was present to make this happen, I was confused. After playing around with different values, I discovered that commenting out display: table
caused the normal behavior I was expecting, e.g. constant column widths.
Now, I can use alternative methods to get the layout I want, that's not a problem; but I am really curious why this is happening. Any thoughts?
EDIT
It looks like this is a webkit bug. display: table
with float and margins works fine in Firefox. Any suggestions on a fix for webkit for posterity?
Further EDIT
I just tested in Safari and it seems to work there as well. WTF Chrome??
Final EDIT
After testing in Firefox 18, Safari, and Chrome Canary (in addition to standard Chrome), it appears that this is in fact a Chrome-specific bug.
The easiest fix is to add a simple additional wrapper div inside each of the ones being floated to contain the content and set the wrappers' CSS to width: 100%; height:100%; display: table;
, then remove the display: table
from the outer elements being floated. Works like a charm.
HTML:
<div class="column">
<div class="sub-element-wrapper">
<div class="sub-element"></div>
<div class="sub-element"></div>
</div>
</div>
<div class="column">
<div class="sub-element-wrapper">
<div class="sub-element"></div>
<div class="sub-element"></div>
</div>
</div>
...
CSS:
.column {
float: left;
width: 15%;
margin: 2%;
/* ... */
}
.sub-element-wrapper {
width: 100%;
height: 100%;
display: table;
}
.sub-element {
display: table-cell;
/* ... */
}