Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:27:05+00:00 2026-06-15T05:27:05+00:00

I have an algorithm for reducing a font size in a given text box

  • 0

I have an algorithm for reducing a font size in a given text box if the text is too big for the box (fit to box)

and it works just fine, but it’s really inefficient as it’s literally just reducing the font size by 1 till it fits.

Can anyone help me make it more efficient, such as binary chopping or something?

the code is:

function fitToBox(object, maxFontSize, minFontSize, maxHeight) {
    var box = document.getElementById(object);
    var currentHeight = box.offsetHeight;                       
    var currentFontSize = maxFontSize;                  
    do {
        box.style.fontSize = currentFontSize+"pt";
        currentFontSize = box.style.fontSize.replace("pt", "");
        currentHeight = box.offsetHeight;                                                       
        if(currentHeight >= maxHeight) {
            currentFontSize -= 1;
        }
    } while (currentHeight > maxHeight && currentFontSize > minFontSize);
    box.style.lineHeight = currentFontSize+"pt";
}

Complete example in HTML (just add more text to the div to see the fitting in progress)

<html>
<head>
<script type="text/javascript">
function fitToBox(object, maxFontSize, minFontSize, maxHeight) {
    var box = document.getElementById(object);
    var currentHeight = box.offsetHeight;                       
    var currentFontSize = maxFontSize;                  
    do {
        box.style.fontSize = currentFontSize+"pt";
        currentFontSize = box.style.fontSize.replace("pt", "");
        currentHeight = box.offsetHeight;                                                       
        if(currentHeight >= maxHeight) {
            currentFontSize -= 1;
        }
    } while (currentHeight > maxHeight && currentFontSize > minFontSize);
    box.style.lineHeight = currentFontSize+"pt";
}
</script>
</head>
<body>

<div id="fitme" style="background-color: yellow; font-size: 24pt; width: 400px;">
    This is some text that is fit into the box This is some text that is fit into the box This is some text that is fit into the box
</div>

<script type="text/javascript">
    fitToBox("fitme", 24, 4, 80);
</script>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:27:06+00:00Added an answer on June 15, 2026 at 5:27 am

    Here’s a version which reduces the loop count from 9 in your example to 4:

    function fitToBox(box, maxFontSize, minFontSize, maxHeight) {
        inc = (maxFontSize - minFontSize) / 2;
        currentFontSize = minFontSize + inc;
        box.style.fontSize = currentFontSize+"pt";
        while(inc >= 1) {
            if(box.offsetHeight > maxHeight) {
                dir = -1;
            } else {
                dir = 1;
            }
            inc = Math.floor(inc/2);
            currentFontSize += (dir * inc);
            box.style.fontSize = currentFontSize+"pt";
        }
    }
    

    It starts by assuming the midway size is a good start and binary chops its way to the best fit below the maximum, changing direction as required.

    I can’t work out why you’re changing the CSS line-height so I haven’t done that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an algorithm to create a PDF file, but I want just to
I have an algorithm that works perfectly, but it uses recursion. I know there
I have an algorithm and I want to measure the time it works, but
I have implemented a nice algorithm ( Non Local Means ) for reducing noise
I have an algorithm about chat program in android, but I have a problem
i have an algorithm that generates permutations of a given word. I'm trying to
I have this algorithm that I found here, just one thing puzzles me about
I have algorithm of calculating average speed in pure python: speed = [...] avg_speed
I have algorithm of calculation of the difference between neighboring elements in pure python:
I have algorithm/computation in Java and unit test for it. The unit test expects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.