Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've done this in the past using jQuery. You can measure the size of a piece of text like this:</p> <pre><code>// txt is the text to measure, font is the full CSS font declaration, // e.g. "bold 12px Verdana" function measureText(txt, font) { var id = 'text-width-tester', $tag = $('#' + id); if (!$tag.length) { $tag = $('&lt;span id="' + id + '" style="display:none;font:' + font + ';"&gt;' + txt + '&lt;/span&gt;'); $('body').append($tag); } else { $tag.css({font:font}).html(txt); } return { width: $tag.width(), height: $tag.height() } } var size = measureText("spam", "bold 12px Verdana"); console.log(size.width + ' x ' + size.height); // 35 x 12.6 </code></pre> <p>In order to fit this to a given space, it's a little trickier - you need to separate out the <code>font-size</code> declaration and scale it appropriately. Depending on how you're doing things, this might be easiest if you break out the different parts of the <code>font</code> declaration. A resize function might look like this (again, obviously, this is jQuery-dependent):</p> <pre><code>function shrinkToFill(input, fontSize, fontWeight, fontFamily) { var $input = $(input), txt = $input.val(), maxWidth = $input.width() + 5, // add some padding font = fontWeight + " " + fontSize + "px " + fontFamily; // see how big the text is at the default size var textWidth = measureText(txt, font).width; if (textWidth &gt; maxWidth) { // if it's too big, calculate a new font size // the extra .9 here makes up for some over-measures fontSize = fontSize * maxWidth / textWidth * .9; font = fontWeight + " " + fontSize + "px " + fontFamily; // and set the style on the input $input.css({font:font}); } else { // in case the font size has been set small and // the text was then deleted $input.css({font:font}); } </code></pre> <p>You can see this in action here: <a href="http://jsfiddle.net/nrabinowitz/9BFQ8/5/" rel="noreferrer">http://jsfiddle.net/nrabinowitz/9BFQ8/5/</a></p> <p>Testing seems to show that this is a little jumpy, at least in Google Chrome, because only full-integer font sizes are used. You might be able to do better with a <code>em</code>-based font declaration, though this might be a little tricky - you'd need to ensure that the <code>1em</code> size for the text width tester is the same as that for the input.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload