I have a div with a known height, in px. In this div, I have some text with a known font-size, in px. Now I want to use padding-top to push the text down so it touches the element beneath it. However, it must not extend the div by pushing too far.
How do I calculate the padding? Just subtracting font-size from div-height, causes the div to expand, even if I set line-height:100%. Is there a defined height based on font – is it always the same? Or does it depend on the font-family. I guess it must depend on the font-weight, at least.
(Don’t want to use position:absolute to solve the pushing down problem.)
It varies depending on the font family. Each font has a different baseline. If you set the height of the container to match the height of the font then the font will still hang below the container. This can be centered using line-height, however there will still be some space present above and below the font.
Here is an article which explains this better.
http://blog.typekit.com/2010/07/14/font-metrics-and-vertical-space-in-css/
DEMO:
Also here is demo I started. You can easily tell where the baseline is and how much extra space there is on top. When line-height is set to equal font-size it centers the font vertically. However, as you can see there is still some white space on top and bottom. So the font itself is not exactly the same height as the container.
HTML
CSS
FIDDLE URL:
http://jsfiddle.net/KtY4F/9/