You can use line-height: 50px; , you will not need vertical-align: middle; .
Demo
The above will fail if you have multiple lines, so in this case you can wrap the text with span and use display: table-cell; and display: table; along with vertical-align: middle; also remember to use width: 100%; for #abc
Demo
#abc{ font:Verdana, Geneva, sans-serif; font-size:18px; text-align:left; background-color:#0F0; height:50px; display: table; width: 100%; } #abc span { vertical-align:middle; display: table-cell; }
Another solution I can come up with here is to use the transform property with translateY() , where Y is obviously the Y-axis. It is pretty simple. All you have to do is set the position of the elements to absolute and a later position 50% of top and translate the axis with negative -50%
div { height: 100px; width: 100px; background-color: tomato; position: relative; } p { position: absolute; top: 50%; transform: translateY(-50%); }
Demo
Please note that this will not be supported in older browsers such as IE8, but you can make IE9 and other older versions of the Chrome and Firefox browser using the -ms, -moz and -webkit respectively.
More information on transform can be found here.
Mr. Alien Sep 06 '13 at 2:54 on 2013-09-06 02:54
source share