vertical-align

The vertical-align property in CSS controls how elements set next to each other on a line are lined up.

img {
  vertical-align: middle;
}

In order for this to work, the elements need to be set along a baseline. As in, inline (e.g. <span>, <img>) or inline-block (e.g. as set by the display property) elements.

The valid values are:

  • baseline - This is the default value.
  • top - Align the top of the element and its descendants with the top of the entire line.
  • bottom - Align the bottom of the element and its descendants with the bottom of the entire line.
  • middle - Aligns the middle of the element with the middle of lowercase letters in the parent.
  • text-top - Aligns the top of the element with the top of the parent element's font
  • text-bottom - Aligns the bottom of the element with the bottom of the parent element's font.
  • sub - Aligns the baseline of the element with the subscript-baseline of its parent. Like where a <sub> would sit.
  • super - Aligns the baseline of the element with the superscript-baseline of its parent. Like where a <sup> would sit.
  • length - Aligns the baseline of the element at the given length above the baseline of its parent. (e.g. px, %, em, rem, etc.)

You can see examples of each here:

Check out this Pen!

A common use case is lining up an avatar with a username. To get them centered along a line, you'd use vertical-align: middle;. Although note that it centers the text according to its tallest ascender and deepest descender.

Each element lines up according to the line you've set, which doesn't change from element to element. So, you can mix-and-match which elements have which value - the elements don't affect each other.

Note that vertical-align is useful on table-cell elements as well, aligning the content within them. Sticking to top, middle, and bottom is the best bet though, as the other values have inconsistent cross-browser results.

More Information

  • What is vertical-align?
  • This property does not allow you to "vertically center" an element within another element. Flexbox is more of the proper tool there. However, there is a trick involving a pseudo "ghost" element that can allow this to work.
  • MDN

Browser Support

Chrome Safari Firefox Opera IE Android iOS
Any Any Any 4+ 4+ Any Any
Fairly consistent across browsers old and new, assuming the font is the same.

Note that some replace elements (e.g. <textarea>) are inline, but their baseline isn't specified, so behavior may vary from browser to browser.

Comments

  1. User Avatar
    Nick Mitchell
    Permalink to comment#

    Hey Guys, Checkout Chris’ artical on Vertical-align here: http://css-tricks.com/what-is-vertical-align

    N

  2. User Avatar
    Srvnk
    Permalink to comment#

    thanks, your lecture works great..

  3. User Avatar
    Devilal
    Permalink to comment#

    Nice explanation. Thanks.

  4. User Avatar
    Daneeshgah
    Permalink to comment#

    Thanks, very helpfull

  5. User Avatar
    Nezar Fadle
    Permalink to comment#

    Thanks for the amazing article :)

    How come the property name is vertical-align and a few of the property values starts with text ( text-top, text-bottom )

    What a naming convention :(

  6. User Avatar
    Bud
    Permalink to comment#

    What is the reason for > * following the class selector in the css rule on the demos?

  7. User Avatar
    Mark
    Permalink to comment#

    What does “In order for this to work, the elements need to be set alone a baseline.” mean??

    Even with the example, for people learning CSS, this sentence does not make sense. The problem is not what baseline means, its the grammar of the sentence that throws everything off.

    Can you please explain?

    • User Avatar
      Mark Robinson
      Permalink to comment#

      alone should be along

    • User Avatar
      Grant
      Permalink to comment#

      I pretty sure you just mistyped, but it says it needs to be set ‘along’ a baseline. This means that the element that you want to vertically align needs a surrounding element with which to align itself with in some way. For instance, if you have an inline <img> element (image element) within a <p> element (paragraph element), the text of the paragraph would be considered a baseline that you can vertically align the image with. You can’t simply stick an image inside of a <div> element and then try to move the image within that element with the vertical-align property. It won’t do anything. Now if you did:

      <div>
        Hello World! 
        <img>
      </div>
      

      You use the text ‘Hello World!’ as a baseline around which you align your img. This is an old question, I know, but hopefully this helps someone in the future.

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag