25

Please consider the following HTML code:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>test</title>
    <style>
        .section {
            display: inline-block;
            border: 1px dashed blue;
        }
        .outer {
            border: 1px dashed red;
            margin: 10px;
        }
    </style>
</head>
<body>
    <div style="height: 500px; width: 200px;" class="section">a
        <div style="height: 100px;" class="outer">1A<br />1B</div>
    </div>
    <div style="height: 500px; width: 200px;" class="section">b
        <div style="height: 200px;" class="outer">2</div>
        <div style="height: 200px;" class="outer">3<br />4<br />5</div>
    </div>
</body>
</html>

Since both divs with class ".section" are the same height, and are inline-blocks, I would expect them to be both vertically aligned. However, the first of these divs is pushed down, so that the text "1B" is aligned with the text line "5" from the other section. Adding or removing lines in either div has a direct impact on the vertical position of my first section.

I don't see the logic of this, and I can't find the answer in the official CSS3 documentation either. Yet, it does not seem to be a bug, as it's identical in Chrome 8, Safari 5, Opera 9.5 and Firefox 4 beta ... didn't try IE, since it's not a reference anyway.

I'm looking for a rational explanation for this phenomenon. Of course there are several workarounds (e.g. changing inline-block to inline-table fixes the problem, or I could use normal floating blocks, etc ...). However I'm trying to comprehend this behaviour.

Hopefully there is someone wiser than myself out there who can explain this.

Live example here.

Kyle
  • 65,599
  • 28
  • 144
  • 152
Eric J. Francois
  • 682
  • 1
  • 7
  • 14
  • 1
    Why are you mixing external and inline styling? – Kyle Jan 28 '11 at 12:05
  • To help with testing this, I've created a [JSFiddle page](http://jsfiddle.net/QfPKD/) using the original code (slightly modified to use fewer in-line styles, but the same effect). – Spudley Jan 28 '11 at 12:08
  • I did the same thing :D Except didn't modify the inline styles. – Kyle Jan 28 '11 at 12:12
  • The mix of external and inline styling is a byproduct from the fact that the code is created in a more complex environment at runtime via a javascript. The inline styles are set as needed on the fly, the others are fix. – Eric J. Francois Jan 28 '11 at 12:19

1 Answers1

68

The default value for vertical-align in CSS is baseline. This means that the baseline of the last text in the first section ("1B") is lined up with the baseline of the last text in the second section ("5") - and also with the baseline of any surrounding text if you had any.

If you add an explicit vertical-align: bottom; to your .section CSS then that will use the bottom of the inline-block as the alignment guide, giving the result you want.

See http://www.brunildo.org/test/inline-block.html for a demonstration of how vertical-align applies to inline blocks

Gareth
  • 133,157
  • 36
  • 148
  • 157
  • Indeed. I was totally oblivious to way vertical-align baseline works. I should not have been reading the documentation of the display property, but of the vertical-align property. Thanks a lot! – Eric J. Francois Jan 28 '11 at 12:27
  • 2
    This is hopefully obvious to anyone reading this, but vertical-align: top; works well too if you are having the opposite problem where subsequent columns are "pushed down" – Brandon Barkley Feb 06 '15 at 16:56