Why my inline-block divs are not aligned when only one of them has text?
alignment, css, html
Solution
This is the consequence of the "baseline" vertical alignment in CSS. From the CSS 2.1 spec, section 10.8 Line height calculations: the 'line-height' and 'vertical-align' properties
baseline
Align the baseline of the box with the baseline of the parent box. If the box does not have a baseline, align the bottom margin edge with the parent's baseline. (my emphasis)
Because the default alignment for the inline-blocks is "baseline", unless it is overridden, this rule applies. When text is put in the inline-block, that text will create a baseline for the inline-block and the first (non-bolded) sentence applies.
When there is no text in the inline-block, then it has no baseline and so the second (bolded) sentence applies.
In the JSFiddle here: http://jsfiddle.net/WjCb9/1/ I have removed from your example the `margin:1em` which was creating (at least for me) a misleading illusion, and added the text `baseline` to show where the baseline of the containing box is. The baseline is along the bottom of the word "baseline", so you can see that the empty inline-block has its bottom margin edge aligned with the parent's baseline as required by the CSS rule above.
Problem
Live page here. Given this HTML page: ``` section[role=main] { margin: 1em; width: 95%; border: 1px solid #999; } section[role=main] article { width: 40%; height: 180px; margin: 1em; display: inline-block; border: 1px solid black; } ``` ``` <section role="main"> <article>Java</article> <article></article> </section> <section role="main"> <article>Java</article> <article>JavaScript</article> </section> ``` I expect both of my articles to be aligned, but as it can be seen in the screenshot below, only when both of my articles have text the `<article>` elements are center aligned: Any ideas what is causing this behavior and how can it be fixed?