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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:52:17+00:00 2026-05-13T15:52:17+00:00

I’ve got a JS-generated fill-in-the-gap text/ cloze and I’m having trouble adjusting the text

  • 0

I’ve got a JS-generated fill-in-the-gap text/cloze and I’m having trouble adjusting the text boxes to the right size.

But unlike others I’m in the position of knowing exactly what the user will/should enter.

So, if I have a gap _______________ like this, I want the input to be exactly 4 characters wide. However, maybe since I’m using a proportional font (and that won’t change), the width is always too large (even for a succession of capital Ds which are pretty wide).

So, what do you suggest? I tried setting the width with size, CSS width in em (too big) and ex (too narrow even for xxes).

I could calculate the width of the actual word (the one that needs to be filled in) a hidden span element, but that seems inelegant.

Is there a way to make the browser have a more accurate guess at the width of the input when I’m using a proportional font?

  • 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-05-13T15:52:17+00:00Added an answer on May 13, 2026 at 3:52 pm

    Monospaced Font

    The best results I’ve seen came through using a monospace font:

    <input type="text" size="4" style="font-family:monospace" />
    

    Online Example: http://jsbin.com/epagi/edit Rendered neatly in Firefox, Chrome, Safari, and IE.

    If you’re using a variable-width font, you would have to use scripting to get a better guess as to what the expected width would be, but as you said, this isn’t very elegant.

    Variable-Width Font

    I tried to work up a reasonable-simple solution for variable-width fonts, but ultimately you will need to see if it fits your project or not.

    Basically what I did was set the text-transform of particular inputs to uppercase to get a semi-consistent expectation for how wide something will be when filled out with text. I then applied a classname that indicated the field should be auto-sized, and how many chars we’re expecting: sizeMe-4. Using jQuery, I collected all of these inputs, and split this classname to get the number of chars expected.

    I extended the String object to include a repeat method which allows me to easily create a string of the expected size, and add it to an ad-hoc span element to get the width. This width was then retroactively applied to the initial input element. The span is then discarded.

    Online Demo: http://jsbin.com/epagi/2/edit

    For convenience, here’s the code:

    <input type="text" name="pin" maxlength="4" class="sizeMe-4" 
           style="text-transform:uppercase" />
    

    —

    String.prototype.repeat = function(num) {
        return new Array( num + 1 ).join( this );
    }
    
    $(function(){
      $(":input[class^='sizeMe']").each(function(){
        var size = Number($(this).attr("class").split("-").pop());
        var newW = $("<span>").text( "X".repeat(size) ).appendTo("body");
        $(this).width( $(newW).width() );
        $(newW).remove();
      });
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a reasonable size flat file database of text documents mostly saved in
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.