views:

97

answers:

4
<html>
  <head>
  </head>
  <body>
    <input type="text" value="1" style="min-width:1px;" />
  </body>
</html>

This is my code and it is not working. Is there any other way in HTML, JavaScript, PHP or CSS to set minimum width?

Clarification by Marcel: The OP wants a text input field with a dynamically changing width, so that the input field fluids around its contents.

+1  A: 

I think you're misinterpreting the min-width CSS property. min-width is generally used to define a minimum DOM width in a fluid layout, like:

input {
  width: 30%;
  min-width: 200px;
}

That would set the input element to a minimum width of 200 pixels. In this context, "px" stands for "pixels".

Now, if you're trying to check to make sure that input field contains at least one character when a user submits it, you'll need to do some form validation with JavaScript and PHP. If that is indeed what you're attempting to do, I'll edit this answer and do my best to help you out.

peterjmag
i know what px means and i know javascript has a function for min-width but it ain't working for input text.
Kerc
@Kerc: “javascript has a function for min-width” - which one? What do you mean? – “it ain't working” is never a good description of a problem. Try to elaborate on the intended behaviour.
Marcel Korpel
`<style type="text/css">``input {` `width: 1%;` `min-width: 1px;``}``</style>`Is always much more
Kerc
Did you even try my example, than you see that it ain't 1 px and yours ain't also. This ain't going to work no matter if i write it in html, css
Kerc
Perhaps there is a php function for this, anybody knows ?
Kerc
I did try your example, but I assumed that a 1 *pixel* wide input field wasn't what you were looking for. If it is, try `<input type="text" value="1" style="width:1px;" />`
peterjmag
than it won't expand and when i print out my calculation something will miss
Kerc
I see what you're trying to do now, and Tahbaza's and user338128's answers seem to fit the bill. In the future, please be more clear with the effect that you're trying to achieve. Your original question gave no indication that you wanted the input field to expand as the user typed.
peterjmag
+1  A: 

You could do something like this

// HTML
<input id="input" type="text" style="width:3px" />
// jQuery
$(function(){
  $('#input').keyup(function(){
    $('<span id="width">').append( $(this).val() ).appendTo('body');
    $(this).width( $('#width').width() + 2 );
    $('#width').remove();
  });
});

+1  A: 

It sounds like your expectation is that the style be applied dynamically to the width of the textbox based on the contents of the textbox. If so you will need some js to run on textbox contents changing, something like this:

<input id="txt" type="text" onkeypress="this.style.width = ((this.value.length + 1) * 8) + 'px';">
Tahbaza
this looks good, i tested it and it works, nice trick
Kerc
Marcel Korpel
I agree, Marcel (and the posted code is not something I can see a use for really) but it exhibits the desired behavior. Do you know how to calculate the actual width of rendered text taking into account font, size, weight, kerning, etc.? If so, please share as I would find that bit of code useful on occasion.
Tahbaza
@Tahbaza – I already thought that it was only an example. It's not that difficult to calculate the width of the input, though. Have a look at my answer.
Marcel Korpel
@Marcel +1 on your answer for sharing, thanks
Tahbaza
+1  A: 

To calculate the width of the current input, you'll have to embed it in a temporary span element, attach that thing to the DOM, get the computed width (in pixels) using the scrollWidth property and remove the span again. Of course you'll have to ensure that the same font family, font size, etc., is used in the input as well as in the span element. Therefore I assigned the same class to them.

I attached the function to the keyup event, as on keypress the input character is not yet added to the input value, so that will result in the wrong width. Unfortunately, I don't know how to get rid of the scrolling of the input field (when adding characters to the end of the field); it scrolls, because the character is added and shown before adjustWidthOfInput() is called. And, as said, I can't do this the other way round because then you'll have the value of the input field before the pressed character is inserted. I'll try to solve this issue later.

BTW, I only tested this in Firefox (3.6.8), but you'll get the point, I hope.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Get/set width of &lt;input&gt;</title>
    <style>
      body {
        background: #666;
      }

      .input-element {
        border: 0;
        padding: 2px;
        background: #fff;
        font: 12pt sans-serif;
      }

      .tmp-element {
        visibility: hidden;
      }
    </style>
  </head>
  <body>
    <input id="theInput" type="text" class="input-element" value="1">
    <script>
      var inputEl = document.getElementById("theInput");

      function getWidthOfInput() {
        var tmp = document.createElement("span");
        tmp.className = "input-element tmp-element";
        tmp.innerHTML = inputEl.value.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
        document.body.appendChild(tmp);
        var theWidth = tmp.scrollWidth;
        document.body.removeChild(tmp);
        return theWidth;
      }

      function adjustWidthOfInput() {
        inputEl.style.width = getWidthOfInput() + "px";
      }

      adjustWidthOfInput();
      inputEl.onkeyup = adjustWidthOfInput;
    </script>
  </body>
</html>

EDIT: Hey, wait, this is more or less the same answer as user338128 gave. Didn't notice it before, though.

Marcel Korpel