Skip to content Skip to sidebar Skip to footer

Align-middle Question

What is my misunderstanding about the way that vertical-align:middle; works? I have posted (http://jsfiddle.net/D6RwZ/) some code which I would expect to vertically align a red rec

Solution 1:

vertical-align:middle won't work on div (block element). You can refer here for details.

If you want to vertical align, I think the only option is using margin/padding with appropriate parameters.


Solution 2:

Vertical-align only works on inline images and display: table-cell.

I've used this solution a few times and it works quite well but takes some work. If you're working with fixed size elements position absolute is by far the simplest. Dynamic sized elements and vertical centering can be very tricky, lots of browser quirks to deal with.


Solution 3:

vertical-align can only be applied to elements with:

  • display:table-cell in order to vertically align the contents of the element.
  • display:inline or display:inline-block in order to vertically align the element within the text line that contains it

A cheap hack with the latter : http://jsfiddle.net/8bZQS/


Post a Comment for "Align-middle Question"