Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I vertically fill a container with uppercase text using JS/CSS?

Tags:

javascript

css

I have a text that is uppercase, e.g. ABC.
As it is uppercase, all characters have the same height.
I also have a container (div) with fixed height, e.g. 100px.

How do I make this text fill it vertically, so each letter is exactly 100 pixels high?

I tried font-size: 100px, but it does not fill the container (there are gaps above and below).
See http://jsfiddle.net/6z8un/1/ for an example.

UPDATE 1:
Let's assume all characters actually have the same height (difference either does not exist or is negligible). Otherwise the question does not make much sense.

UPDATE 2:
I am pretty sure it can be solved using https://stackoverflow.com/a/9847841/39068, but so far I had no perfect solution with it. I think ascent and descent are not enough, I would need something else for the top space.

like image 337
Andrey Shchekin Avatar asked Nov 02 '22 08:11

Andrey Shchekin


1 Answers

line-height http://jsfiddle.net/6z8un/2/ will not solve the problem because this will not remove the whitespaces. You could apply the size by hardcoding (for me it fits with font-size of 126px) But this is different to every user (sans-serif can be configured by user/system/browser)

Windows default sans-serif font MS sans serif is different to Droid sans serif on Android or DejaVu Sans on Ubuntu.

To solve this problem, you could set a font to default, like Times New Roman, but not every system does have this font by default.

To solve this, you could use a custom font imported from a server like htttp://google.com/fonts but not every browser does support custom fonts.

I think the only way to solve this is to use an image.

But custom fonts should do their job on modern browsers too :) (e.g.: http://jsfiddle.net/6z8un/5/ )

like image 167
bbuecherl Avatar answered Nov 08 '22 13:11

bbuecherl