Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this inline-block element have content that is not vertically aligned

Came across a weird CSS issue. Can someone explain why the box having content is not vertically aligned?

If you put text inside the span with class .divPutTextToFixIssue - it aligns properly.

http://jsfiddle.net/KgqJS/88/

   #divBottomHeader {          background-color: #d5dbe0;          height: 43px;      }      .divAccountPicker {          display: inline-block;          background: blue;                      width: 200px;          height: 40px;      }      .divAccountData {          display: inline-block;          background: red;          width: 400px;          height: 40px;      }
<div id="divBottomHeader">          <div class="divAccountPicker">             <span class="divPutTextToFixIssue"></span>                           </div>          <div class="divAccountData">              <span>Balance: $555</span>          </div>      </div>
like image 500
Anton Skovorodko Avatar asked Oct 18 '12 08:10

Anton Skovorodko


People also ask

How do you vertically align text in inline block elements?

The vertical-align property in CSS controls how elements set next to each other on a line are lined up. 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.

How do you align inline block elements?

To align things in the inline direction, use the properties which begin with justify- . Use justify-content to distribute space between grid tracks, and justify-items or justify-self to align items inside their grid area in the inline direction.

How do you align block elements vertically?

Vertical-Align When the element is a block level element we give it a width and set the left and right margins to a value of auto. With text-align: center in mind, most people look first to vertical-align in order to center things vertically.

How do you vertically align text in an element?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .


1 Answers

The default vertical-align value is baseline which

Aligns the baseline of the box with the baseline of the parent box

Note: You can see this default value in action by adding vertical-align:baseline to your .divAccountData selector. Since baseline is default the alignment is unchanged.

You need to change it to top to align the blocks at the top, for example:

.divAccountData {     display: inline-block;     background: red;     width: 400px;     height: 40px;     vertical-align: top; } 

Baseline is defined as

the line upon which most letters "sit" and below which descenders extend

To address why adding text seems to fix the problem it is because

The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the baseline is the bottom margin edge.

from CSS2 Visual formatting model details

So adding just a single character changes the baseline, causing the second block to appear at the same vertical alignment. This only works if both blocks contain the same number of lines. Try adding more words to one of your blocks and you will see that without forcing vertical-align:top on the second block it will move depending on how many lines of text exist in the first block.

Edit: Found a related question Why is this inline-block element pushed downward?

like image 136
andyb Avatar answered Sep 21 '22 22:09

andyb