181

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>
mike
  • 46,876
  • 44
  • 102
  • 112

10 Answers10

257

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

Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
124

Be aware that the line-height approach fails if you have a long sentence in the span which breaks the line because there's not enough space. In this case, you would have two lines with a gap with the height of the N pixels specified in the property.

I stuck into it when I wanted to show an image with vertically centered text on its right side which works in a responsive web application. As a base I use the approach suggested by Eric Nickus and Felipe Tadeo.

If you want to achieve:

desktop

and this:

mobile

.container {
    background: url( "https://i.imgur.com/tAlPtC4.jpg" ) no-repeat;
    display: inline-block;
    background-size: 40px 40px; /* image's size */
    height: 40px; /* image's height */
    padding-left: 50px; /* image's width plus 10 px (margin between text and image) */
}

.container span {
    height: 40px; /* image's height */
    display: table-cell;
    vertical-align: middle;
}
<span class="container">
    <span>This is a centered sentence next to an image</span>
</span>
lluisaznar
  • 2,383
  • 1
  • 15
  • 14
46

This is the simplest way to do it if you need multiple lines. Wrap you span'd text in another span and specify its height with line-height. The trick to multiple lines is resetting the inner span's line-height.

<span class="textvalignmiddle"><span>YOUR TEXT HERE</span></span>
.textvalignmiddle {
    line-height: /*set height*/;
}

.textvalignmiddle > span {
    display: inline-block;
    vertical-align: middle;
    line-height: 1em; /*set line height back to normal*/
}

DEMO

Of course the outer span could be a div or whathaveyou

Hashbrown
  • 12,091
  • 8
  • 72
  • 95
  • 2
    `span`s, like any other inline element, [can contain any HTML inline element](https://stackoverflow.com/questions/5545884/can-span-tags-have-any-type-of-tags-inside-them#5545914). Regardless, I wrote that `textvalignmiddle` can be a `div` if so necessary (and you can set it to `display:inline;` if you're so averse to using `spans`) – Hashbrown May 22 '15 at 01:20
  • I mean't they shouldn't have. They are for containing text. Else use a div. – JorgeeFG May 22 '15 at 13:31
  • 3
    `span` is the [*generic* inline container](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span) for html and doesnt so much as hint at being text only; ` It can be used to group **elements** for styling purposes...`. I suggest reading the spec before pushing your personal coding standards on others stating them as fact – Hashbrown May 23 '15 at 15:25
  • 3
    either way, this line of conversation isnt constructive in a technical sense to the question and isnt what comments in SO are for – Hashbrown May 23 '15 at 15:27
  • Works for me too. Used a different element than span. – chocolata Aug 27 '15 at 18:57
  • Doesn't work for me when using the following doctype tag: – Yury Kozlov Jan 30 '17 at 11:02
  • If you're actually using transitional elements, why bother with `doctype`s in the first place, IMO. Any reason why you need to declare your document transitional? – Hashbrown Jan 31 '17 at 00:28
44

The flexbox way:

.foo {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
}
Chris Clower
  • 5,036
  • 2
  • 18
  • 29
5

I needed this for links, so I wrapped the span with an a-tag and a div, then centered the span tag itself

HTML

<div>
    <a class="tester" href="#">
        <span>Home</span>
    </a>
</div>

CSS

.tester{
    display: inline-block;
    width: 9em;
    height: 3em;
    text-align: center;
}
.tester>span{
    position: relative;
    top: 25%;
}
Jason
  • 872
  • 9
  • 14
  • Thanks for this, I hate messing with CSS, I was spending all day trying to vertically center an element when I came across your answer here. You're a life saver! – Todd Nestor Jan 12 '15 at 08:07
5

CSS vertical center image and text

I have Create one demo for vertical image center and text also i have test on firefox ,chrome,safari, internet explorer 9 and 8 too.

It is very short and easy css and html, Please check below code and you can find output on screenshort.

HTML

<div class="frame">
    <img src="capabilities_icon1.png" alt="" />
</div>

CSS

  .frame {
        height: 160px;      
        width: 160px;
        border: 1px solid red;
        white-space: nowrap;
        text-align: center; margin: 1em 0;
    }

    .frame::before {
        display: inline-block;
        height: 100%;
        vertical-align: middle;
        content:"";
    }

    img {
        background: #3A6F9A;
        vertical-align: middle;
    }

Output enter image description here

Eduard Jacko
  • 1,974
  • 1
  • 16
  • 29
2

this works for me (Keltex said the same)

.foo {
height: 50px;
...
}
.foo span{
vertical-align: middle; 
}

<span class="foo"> <span>middle!</span></span>
Philll_t
  • 4,267
  • 5
  • 45
  • 59
2

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

align-self: center;

More info

Pedro Bezanilla
  • 1,167
  • 14
  • 22
1

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
  • 26,220
  • 11
  • 79
  • 111
1

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
  • 22,558
  • 19
  • 94
  • 162