By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,532 Members | 1,514 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,532 IT Pros & Developers. It's quick & easy.

dynamically determine the largest possible font size

P: n/a
hi all-

i'm a totally js hack so go easy on me...

i'd like to create a function that, given the size of a block of text
in

x = maximum number of chars
y = total number of lines (this is in chars too of course)

could determine and set the font size to be close, say 90 percent, to
the maximum that will display in the users browser without scrolling.
if you know what lightning slides are you'll understand what i'm
doing.

for example, given

foo
bar
foobar

x = 6
y = 3

i am trying to set the font size such that this text would __just__
fit onto the screen without scrolling in horizontal or vertical
directions.

i've got some code that's close, but i'm hoping someone here might
have a more elegant way.

kind regards

-a

Feb 6 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a

ar**********@gmail.com wrote:
hi all-

i'm a totally js hack so go easy on me...

i'd like to create a function that, given the size of a block of text
in

x = maximum number of chars
y = total number of lines (this is in chars too of course)

could determine and set the font size to be close, say 90 percent, to
the maximum that will display in the users browser without scrolling.
if you know what lightning slides are you'll understand what i'm
doing.

for example, given

foo
bar
foobar

x = 6
y = 3

i am trying to set the font size such that this text would __just__
fit onto the screen without scrolling in horizontal or vertical
directions.
Hi,
You can try to define max font size for the container by augmentation
in series until the container size exceeds the document body size. For
example, code snippet(untested):

function getFontSize()
{
var s = document.getElementById("container");
var fontSize = 10;
while(true)
{
s.style.fontSize = fontSize;
if (s.offsetWidth document.body.offsetWidth || s.offsetHeight
document.body.offsetHeight)
{
fontSize--;
alert("Max font size = "+fontSize);
break;
}
fontSize++;
}
}

It is simple an idea.
i've got some code that's close, but i'm hoping someone here might
have a more elegant way.
You didn't post the code you have, so I could not compare elegancy. :)

Feb 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.