19

I need to center vertically multiple boxes with different heights.

I dont know the height of the boxes as they are variable texts.

How can I do this with CSS (without having to use td and valign). Tried display: table-cell but it seems not compatible with IE

The image below shows the design, the post-it is the browser window

removed dead ImageShack link

SuperBiasedMan
  • 9,484
  • 10
  • 45
  • 70
Victor
  • 22,028
  • 28
  • 82
  • 115

2 Answers2

5

Not so elegant, but works. Create one-cell table. Only table has cross-browser vertical-align.

Dewfy
  • 22,648
  • 12
  • 68
  • 116
3

Assuming you want the boxes to be fixed-width you can use the folling markup

<div class="vcontainer">
    <span>1<br/>2</span>
    <span>1<br/>2<br/>3<br/>4</span>
    <span>1<br/>2<br/>3</span>
</div>

with these styles

.vcontainer {
    text-align: center;
}

.vcontainer span {
    display: inline-block;
    width: 150px;
    vertical-align: middle;
}

The inline-block property works in most modern browsers.

Josef Pfleger
  • 73,505
  • 15
  • 96
  • 99
  • This is basically the correct way to do things. However, if you change the three elements to `` instead of `
    ` then you get compatibility with even IE6! For Firefox 2 (if necessary), you can add `display:-moz-inline-box`.
    – DisgruntledGoat Jan 31 '10 at 23:14