If you will try to vertically center align text inside a div using the CSS rule vertical-align: middle; you won't succeed. 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.
The concept behind this trick is, if the value of the line-height property is greater than the value of the font-size for an element, the difference (called "leading") is cut in half and distributed evenly on the top and bottom of the in-line box that align the inline elements vertically center to the element.
Let's try out the following example to understand how it actually works:
Use the CSS
line-heightpropertyIf you will try to vertically center align text inside a div using the CSS rule
vertical-align: middle;you won't succeed. Suppose you have a div element with the height of50pxand you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply theline-heightproperty with value equal to the height of div which is50px.The concept behind this trick is, if the value of the
line-heightproperty is greater than the value of thefont-sizefor an element, the difference (called "leading") is cut in half and distributed evenly on the top and bottom of the in-line box that align the inline elements vertically center to the element.Let's try out the following example to understand how it actually works:
need an explanation for this answer? contact us directly to get an explanation for this answer