18

I was told that:

Vertical align only works for inline,inline-blocks,images,and table elements.
It has to be applied on the child element, as oppose to the parent element, unlike text-align.

However, when I tried to set vertical align middle on an inline-block element, it didn't work. Why?

#wrapper {
border: 1px solid black;
width: 500px;
height: 500px;
}
#content {
border: 1px solid black;
display: inline-block;
vertical-align: middle;
}
<div id = 'wrapper'>
<div id = 'content'> content </div>
</div>
frosty
  • 2,250
  • 7
  • 23
  • 61
  • @Oriol I meant middle. And before you ask, yes, middle is a valid value. http://www.w3schools.com/cssref/pr_pos_vertical-align.asp And what do you mean by line box? – frosty Feb 21 '16 at 01:35
  • @Oriol Also, I'm not looking for a different way to do this. I'm just looking for an explanation for why this doesn't work, so please take off the duplicate mark. – frosty Feb 21 '16 at 01:40
  • @Oriol If I'm understanding this correctly, are you referring to #content as the line box? Because there's only 2 elements here, the #wrapper, and #content. – frosty Feb 21 '16 at 01:54
  • @Oriol ...Okay, so line boxes only exist when there are more than 1 inline, or inline-block elements in a single parent container? But they are invisible, right? Basically, every row of inline-block elements is a line box? If I'm understanding this correctly? – frosty Feb 21 '16 at 02:01

3 Answers3

29

It doesn't work because vertical-align sets the alignment of inline-level contents with respect to their line box, not their containing block:

This property affects the vertical positioning inside a line box of the boxes generated by an inline-level element.

A line box is

The rectangular area that contains the boxes that form a line

When you see some text which has multiple lines, each one is a line box. For example, if you have

p { border: 3px solid; width: 350px; height: 200px; line-height: 28px; padding: 15px; }
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur.</p>

enter image description here

If there is only a single line, like in your case, it's also a line box

enter image description here

Using vertical-align: middle centers .content vertically inside that line box. But the problem is that the line box is not vertically centered inside the containing block.

If you want to center something vertically at the middle of the containing block see How to align text vertically center in div with CSS?

Community
  • 1
  • 1
Oriol
  • 225,583
  • 46
  • 371
  • 457
  • therefore, if its a guaranteed single line content text and the line-height is the same length as its container's height, the content text will be in the middle of the container. ie: `#content{line-height: 500px}` https://jsfiddle.net/28ng9ovs/ – Aizzat Suhardi Dec 31 '16 at 06:39
  • @AizzatSuhardi Yes, and you can use `white-space: nowrap` to prevent line breaks. – Oriol Dec 31 '16 at 17:47
5

Vertical-align on inline / inline-block element, images, text... align element together, not with parent.

Usage example: align smiley image in a text

you can cheat by adding a 0px width, 100% height pseudo-element

#wrapper {
    border: 1px solid black;
    width: 200px;
    height: 200px;
    vertical-align: middle;
}
#wrapper:after{
    content: '';
    display: inline-block;
    width: 0px;
    height: 100%;
    vertical-align: middle;
}
#content {
    border: 1px solid black;
    display: inline-block;
    vertical-align: middle;
}
<div id = 'wrapper'>
    <div id = 'content'> content </div>
</div>
Yukulélé
  • 11,464
  • 8
  • 52
  • 76
2

It work for me by using vertical-align:text-top;

<div class="box">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh 
</div>
<div class="box">
    Lorem ipsum dolor sit amet, diam nonummy nibh 
</div>
<div class="box">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh 
</div>

.box {
   display: inline-block;
   width: 20%;
   margin: 5px;
   padding:10px;
   border-top: 5px solid #000000;
   vertical-align: text-top;
}

jsfiddle

fede
  • 21
  • 1