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,814
  • 10
  • 45
  • 73
Victor
  • 23,172
  • 30
  • 86
  • 125

2 Answers2

5

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

Dewfy
  • 23,277
  • 13
  • 73
  • 121
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
  • 74,165
  • 16
  • 97
  • 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