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 8406641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:09:56+00:00 2026-06-09T23:09:56+00:00

I have an html div that is of a fixed size and an additional

  • 0

I have an html div that is of a fixed size and an additional HTML input box that is used to input text. This text is then displayed in the div.

I want the text to always be centred and always be the largest size possible for the number of characters in the string.

I have come up with a working prototype but it doesn’t work too well. The text is not always correctly sized.

function bindText(readElement, writeElement, extra){   
    //the text from the input form triggered by onkeyup event
    var textStr = document.getElementById(readElement).value;

    //the div to write the text to 
    var element = document.getElementById(writeElement);

    //the major part of the function that I have just brute forced to try and work out
    if(textStr.length > 0){                        

        //check the length of the string
        if(textStr.length > 8 && textStr.length < 12){
             //set a the font size accordingly
             element.style.fontSize = 150;
        }

        //same again    
        if(textStr.length >= 12 && textStr.length < 16){
            element.style.fontSize = 120;
        }

        if(textStr.length >= 16 && textStr.length < 20){
            element.style.fontSize = 100;
        }

        if(textStr.length >= 20 && textStr.length < 25){
            element.style.fontSize = 80;
        }

        if(textStr.length >= 25 && textStr.length < 32){
            element.style.fontSize = 55;
        }

        if(textStr.length >= 32 && textStr.length < 40){
            element.style.fontSize = 50;
        }

        if(textStr.length >= 40 && textStr.length < 50){
            element.style.fontSize = 45;
        }

        if(textStr.length >= 76){
            element = textStr.splice(0, 75);
        }
       }
   }

Is there a more elegant solution to get more consistent results.?

Thanks in advance

  • 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-09T23:09:58+00:00Added an answer on June 9, 2026 at 11:09 pm

    As I can see in your approximation of dependence between font size and length of the text:
    enter image description here

    I made interpolation for this in form:
    enter image description here

    and you can see the results of this with parameters:

    enter image description here

    enter image description here

    Now you can replace all your if and else if by one formula for font size h calculation and couple min(h, max_font_size) and max(min_font_size, h) for setting of the font minimal and maximum size.

    function bindText(readElement, writeElement, extra){   
        var textStr = document.getElementById(readElement).value;
        var element = document.getElementById(writeElement);
        var x, h;
    
        x = textStr.length;
        h = Math.exp(-0.1 * x + 6) + 38;
        h = Math.min(h, 120);
        h = Math.max(h, 45);
    
        element = textStr.splice(0, 75);// as you wish and it is not my business why %)
        element.style.fontSize = h;
    
        return (0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have some HTML that looks like this: <div id=text> This is some
I have html that looks like this with several items <div class=item> <p class=price>$388.00</p>
I have some HTML that looks like this: <div> <div class=value> <a href=# class=clicker>Some
I have some HTML that looks like this: <div class=product> Content In Here <div
I have an HTML page that roughly looks like this: <div id=rolesRollerBody style=height: 309px;>
I want to make a container of fixed size that will inside have an
I have a fixed-size box that is set to scroll when it overflows. However,
I have a div that is created by Ajax with .append(html) function, I'm trying
So I have some html that looks like the following: <div class='something unknown' id='something_unknown_1'>
I have a jquery line of code that outputs some HTML to a div.

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.