0

How can i make my input box length relative to the amount of text in it?

<div class="kontakt">
                <h2>Kontakt oss</h2>
                <p>Hei! Jeg heter <input type="text" name="navn" placeholder="navn">, og jeg lurer på følgende:</p>

CSS:

.kontakt {
margin-top: 4%;
font-size: 80%;
text-align: center;
}
.kontakt input{
    border:none;
    border-bottom: 1px solid black;
    background:none;
    color:white;
    opacity: 0.7;
    text-align: center;
    size: relative;
}
Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41

2 Answers2

0

this will help you:

adjustable text area


$('input[type="text"]').keypress(function(e) {
    if (e.which !== 0 && e.charCode !== 0) { // only characters
        var c = String.fromCharCode(e.keyCode|e.charCode);
        $span = $(this).siblings('span').first();
        $span.text($(this).val() + c) ; // the hidden span takes 
                                        // the value of the input
        $inputSize = $span.width() ; 
        $(this).css("width", $inputSize) ; // apply width of the span to the input
     }
}) ;
Martin
  • 22,212
  • 11
  • 70
  • 132
MyStack
  • 53
  • 9
0

You can try this:-

style='width:20em'

link:-Input size vs width

Community
  • 1
  • 1
Razia sultana
  • 2,168
  • 3
  • 15
  • 20