I have an HTML template with a series of blocks, which are just "inline-block". Initially, a number of blocks are rendered as part of the template, but users may add additional blocks, which then get appended dynamically.
My problem is that the dynamically added blocks have a different spacing compared to the pre-rendered ones.
Check out this fiddle: https://jsfiddle.net/7w3hu5gk/
It is clear that the blocks, added dynamically by the Javascript code, don't line up vertically.
HTML:
<div id="blocks">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
CSS:
#blocks {
width:140px;
}
#blocks div.block {
display:inline-block;
*display:inline; // Legacy IE love
zoom:1;
vertical-align:top;
width:20px;
height:20px;
margin:5px;
border:1px solid red;
background:1px solid #777;
}
It seems that the culprit is the inherent (and invisible) character spacing, since inline-block
makes elements behave sort-of-like text. Setting font-size: 0
on the #blocks
element will magically fix the problem. But then the font size of text contained within the div.blocks
elements have to be resized.
Does anyone have a nice fix for this?
- Floating elements (e.g.
float: left;
) are not a desirable alternative.