Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-scaling input[type=text] to width of value?

Tags:

html

jquery

css

Is there a way to scale the width of an <input type="text"> to the width of the actual value?

input {    display: block;    margin: 20px;    width: auto;  }
<input type="text" value="I've had enough of these damn snakes, on this damn plane!" />    <input type="text" value="me too" />
like image 268
Walker Avatar asked Nov 11 '11 22:11

Walker


People also ask

How do you change the width of input type text?

The size attribute specifies the visible width, in characters, of an <input> element. Note: The size attribute works with the following input types: text, search, tel, url, email, and password. Tip: To specify the maximum number of characters allowed in the <input> element, use the maxlength attribute.

How do I make input width fit content?

Use the span. text to fit width of text, and let the input have same size with it by position: absolute to the container.


2 Answers

You can do this the easy way by setting the size attribute to the length of the input contents:

function resizeInput() {     $(this).attr('size', $(this).val().length); }  $('input[type="text"]')     // event handler     .keyup(resizeInput)     // resize on page load     .each(resizeInput); 

See: http://jsfiddle.net/nrabinowitz/NvynC/

This seems to add some padding on the right that I suspect is browser dependent. If you wanted it to be really tight to the input, you could use a technique like the one I describe in this related answer, using jQuery to calculate the pixel size of your text.

like image 171
nrabinowitz Avatar answered Sep 28 '22 13:09

nrabinowitz


A SIMPLE BUT PIXEL PERFECT SOLUTION

I have seen several ways to do this but calculating the width of fonts isn't always 100% accurate, it's just an estimate.

I managed to create a pixel perfect way of adjusting the input width by having a hidden placeholder to measure from.


jQuery (Recommended)

$(function() {   $('#hide').text($('#txt').val());   $('#txt').width($('#hide').width()); }).on('input', function() {   $('#hide').text($('#txt').val());   $('#txt').width($('#hide').width()); });
body, #txt, #hide {   font: inherit;   margin: 0;   padding: 0; }  #txt {   border: none;   color: #888;   min-width: 10px; }  #txt:focus-visible {   outline: none; }  #hide {   display: none;   white-space: pre; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  <p>Lorem ipsum   <span id="hide"></span><input id="txt" type="text" value="type here ..."> egestas arcu. </p>

Pure JavaScript

I was unable to determine how jQuery calculates the width of hidden elements so a slight tweak to css was required to accomodate this solution.

const hide = document.getElementById('hide'); const txt = document.getElementById('txt'); resize(); txt.addEventListener("input", resize);  function resize() {   hide.textContent = txt.value;   txt.style.width = hide.offsetWidth + "px"; }
body, #txt, #hide {   font: inherit;   margin: 0;   padding: 0; }  #txt {   border: none;   color: #888;   min-width: 10px; }  #txt:focus-visible {   outline: none; }  #hide {   position: absolute;   height: 0;   overflow: hidden;   white-space: pre; }
<p>Lorem ipsum   <span id="hide"></span><input id="txt" type="text" value="type here ..."> egestas arcu. </p>
like image 20
DreamTeK Avatar answered Sep 28 '22 11:09

DreamTeK