Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Wow, this problem is popular. It's based on a misunderstanding in the <code>vertical-align</code> property. This excellent article explains it:</p> <p><a href="http://phrogz.net/CSS/vertical-align/index.html" rel="noreferrer">Understanding <code>vertical-align</code>, or "How (Not) To Vertically Center Content"</a> by Gavin Kistner.</p> <p><strong><a href="http://howtocenterincss.com/" rel="noreferrer">“How to center in CSS”</a></strong> is a great web tool which helps to find the necessary CSS centering attributes for different situations.</p> <hr> <p>In a nutshell <sub><sup>(and to prevent link rot)</sup></sub>:</p> <ul> <li><strong>Inline elements</strong> (and <em>only</em> inline elements) can be vertically aligned in their context via <code>vertical-align: middle</code>. However, the “context” isn’t the whole parent container height, it’s the height of the text line they’re in. <a href="http://jsfiddle.net/jBthq/" rel="noreferrer">jsfiddle example</a></li> <li>For block elements, vertical alignment is harder and strongly depends on the specific situation: <ul> <li>If the inner element can have a <strong>fixed height</strong>, you can make its position <code>absolute</code> and specify its <code>height</code>, <code>margin-top</code> and <code>top</code> position. <a href="http://jsfiddle.net/YFncP/2/" rel="noreferrer">jsfiddle example</a></li> <li>If the centered element <strong>consists of a single line</strong> <em>and</em> <strong>its parent height is fixed</strong> you can simply set the container’s <code>line-height</code> to fill its height. This method is quite versatile in my experience. <a href="http://jsfiddle.net/d4zGF/" rel="noreferrer">jsfiddle example</a></li> <li>… there are more such special cases.</li> </ul></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload