Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically center multiple boxes with CSS

Tags:

html

css

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

like image 984
Victor Avatar asked Jan 31 '10 18:01

Victor


People also ask

How do I align two objects vertically in CSS?

To align two <div> elements vertically in Bootstrap 3, you can try using the CSS Flexible Box Layout. In the example below, we display the needed row as a flex container box with the CSS display property and then, align the flex-items (columns) vertically with the align-items property.

How do you vertically align a box in CSS?

CSS Demo: vertical-alignThe vertical-align property can be used in two contexts: To vertically align an inline element's box inside its containing line box. For example, it could be used to vertically position an image in a line of text. To vertically align the content of a cell in a table.

How do I vertically center all items in a div?

Using the Line-Height Property In most cases, you can also center text vertically in a div by setting the line-height property with a value that is equal to the container element's height.


2 Answers

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

like image 189
Dewfy Avatar answered Oct 15 '22 15:10

Dewfy


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.

like image 28
Josef Pfleger Avatar answered Oct 15 '22 17:10

Josef Pfleger