tags:

views:

518

answers:

4

How do I get the "x" to be vertically-aligned in the middle of the span?

.foo {
    height: 50px;
    border: solid black 1px;
    display: inline-block;
    vertical-align: middle;
}

<span class="foo">
   x
</span>
+3  A: 

EDIT: see http://stackoverflow.com/questions/527811/css-vertical-centering

Snarky answer: Use a table.

The fact is that vertical alignment is shamefully difficult to accomplish using css. First of all, the vertical-align property doesn't work the way your intuition probably tells you that it should. If the details of how to use the vertical-align property interest you, you will find a wealth of information at your nearest google.

There are some css-based techniques which will accomplish vertical centering. Almost all of them require that you specify a fixed height for span.foo. See: Understanding vertical-align, or "How (Not) To Vertically Center Content"

Ken Browning
This is a different case to the post you linked to: that one talks about vertically aligning a block element, but this question is about an inline element.
wheresrhys
+1  A: 

The vertical-align css attribute doesn't do what you expect unfortunately. This article explains 2 ways to vertically align an element using css.

Keltex
+4  A: 

Use line-height:50px; instead of height. That should do the trick ;)

mofle
Yep - much better answer than mine,
wheresrhys
A: 

Set padding-top to be an appropriate value to push the x down, then subtract the value you have for padding-top from the height.

wheresrhys