Roughly speaking, trying to build a four-class layout, I have this HTML:
<div> <div>A column</div> <div>A column</div> <div>A column</div> <div>A column</div> </div>
And I have this CSS:
div { background: #ccc; } div div { background: #eee; display: inline-block; width: 25%; }
-> Shut me on this <-
When rendering in the browser (currently I tested only Chrome), spaces between nested div elements (in this example, spaces are caused by line breaks) are rendered, thereby displaying my layout.
Clearly, I can float my nested divs ...
div { background: #ccc; } div div { background: #eee; width: 25%; float: left; }
-> Tell me that <-
But then my div container collapses, and I don't want to need to use CSS clearfix hacks or extra HTML to open it.
Alternatively, I can change my HTML to remove the space ...
<div><div>A column</div><div>A column</div><div>A column</div><div>A column</div></div>
but it makes work difficult. An alternative to hacking tags so that it becomes more readable, for some reason leaves me feeling dirty ...
<div> <div>A column</ div><div>A column</ div><div>A column</ div><div>A column</div> </div>
I found a resource or two (I couldn’t find anything in SO), but I don’t really like any of the solutions - are they all workarounds that I will entertain if I have to, but is there an alternative for sure?
So my question (s) ... is there a cross-browser, w3c-compatible, non-javascript, no hacking, neat HTML, hack-safe way to prevent HTML scrolling in the browser when using display:inline-block ? Or is there an alternative to a built-in block that can be used that does not have unpleasant side effects?
EDIT
Assuming this is really not possible, the best solution would be to not add HTML markup and “flexible” CSS. In other words, the webmaster could edit the HTML as usual without taking into account the layout break, and CSS (hacked or otherwise) can accommodate the webmaster in corrections without the need to make changes.
MY "WORKAROUND"
Well, it seems like something was supposed to give. In my situation, it’s more important to have HTML that does not require additional markup, so the best solution is to work in a CSS hack that “just works” invisibly. The solution is to float the nested divs and add a hack ...
div div { float: left; } div::before, div::after { content: ""; display: table; } div::after { clear: both; } div { *zoom: 1; }
... which is a derivation of the fix that I have been using for some time and was hoping to avoid. This version of the succint patch was found on this site .
So, now every single div in the markup has received a clearfix fix for it, regardless of whether it is needed or not. I have yet to find out if this has bad side effects by applying to all divs - I look forward to debugging and fixing when there are any problems; -)