1

Possible Duplicate:
Auto-size dynamic text to fill fixed size container.

Let's say i have a div that is 600 px high and 400 px wide. I want my text in the middle of this, and to span the entire width of the div. Is there something in CSS or CSS3 that will let me auto do this based on how much text a user inputs?

so, the word FOO would be larger than the word FOOBAR as foobar would take up more space, thus dynamically resizing that text.

Just wondering; I'm not sure if it's possible (would be acceptable to have a JS solution too..)

Thank you!

Community
  • 1
  • 1
Petrogad
  • 4,405
  • 5
  • 38
  • 77

2 Answers2

1

One solution I can think is:

Let says the max size you can have by just typing word FOO in box is 200px

So create a new hidden div set a fixed width of 600 but not height, then loop through

  1. Create new hidden div
  2. Set a width 600 and leave the height
  3. loop through javascript by reducing the font-size until the height is <= 400
Shishant
  • 9,256
  • 15
  • 58
  • 79
0

I don't see how you can do this for proportional fonts (like Arial) without a lot of effort. Each character has a different width.

You may be able to do this for a "monospace" font like Courier New. You'd have to extract the text, count the number of characters and adjust the font-size accordingly. However different monospace fonts have different widths, so you'd have to have a table of widths versus fonts. However this shouldn't be too difficult to do with a bit of experimentation.

user229044
  • 232,980
  • 40
  • 330
  • 338
Neil
  • 3,229
  • 1
  • 17
  • 15