How do I vertically align something inside a span tag?

Viewed 316638

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>
10 Answers

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

The flexbox way:

.foo {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
}

Just giving an alternative, in case you encapsulate your element in a flexible container (e.g.: display: flex):

align-self: center;

More info

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

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.

Related