HTML input textbox with a width of 100% overflows table cells

前端 未结 14 1508
别那么骄傲
别那么骄傲 2021-01-29 20:28

Does anyone know why the input elements with a width of 100% go over the table\'s cells border.

In the simple example below input box go over the table\'s cells border,

相关标签:
14条回答
  • 2021-01-29 21:05

    The problem has been explained previously so I will only reiterate: width doesn't take into account border and padding. One possible answer to this not discussed but which I have found helped me out a bunch is to wrap your inputs. Here's the code, and I'll explain how this helps in a second:

    <table>
      <tr>
        <td><div style="overflow:hidden"><input style="width:100%" type="text" name="name" value="hello world" /></div></td>
      </tr>
    </table>
    

    The DIV wrapping the INPUT has no padding nor does it have a border. This is the solution. A DIV will expand to its container's size, but it will also respect border and padding. Now, the INPUT will have no issue expanding to the size of its container since it is border-less and pad-less. Also note that the DIV has its overflow set to hidden. It seems that by default items can fall outside of their container with the default overflow of visible. This just ensures that the input stays inside its container and doesn't attempt to poke through.

    I've tested this in Chrome and in Fire Fox. Both seem to respect this solution well.

    UPDATE: Since I just got a random downvote, I would like to say that a better way to deal with overflow is with the CSS3 box-sizing attribute as described by pricco:

    .myinput {
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        -ms-box-sizing: border-box;
        -o-box-sizing: border-box;
        box-sizing: border-box;
    }
    

    This seems to be pretty well supported by the major browsers and isn't "hacky" like the overflow trick. There are, however, some minor issues on current browsers with this approach (see http://caniuse.com/#search=box-sizing Known Issues).

    0 讨论(0)
  • 2021-01-29 21:05

    I fixed this issue starting with @hallodom's answer. All my inputs were contained within li's, so all I had to do was set the li overflow:hidden for it to remove that excess input overflow.

    .ie7 form li {
      width:100%;
      overflow:hidden;
    }
    
    .ie7 input {
      width:100%;
    }
    
    0 讨论(0)
  • 2021-01-29 21:08

    With some Javascript you can get the exact width of the containing TD and then assign that directly to the input element.

    The following is raw javascript but jQuery would make it cleaner...

    var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++)
    {
        var el = inputs[i];
        if (el.style.width == '100%')
        {
            var pEl = el.parentNode;  // Assumes the parent node is the TD...
            el.style.width = pEl.offsetWidth;
        }
    }
    

    The issues with this solution are:

    1) If you have your inputs contained in another element such as a SPAN then you will need loop up and find the TD because elements like SPANs will wrap the input and show its size rather then being limited to the size of the TD.

    2) If you have padding or margins added at different levels then you might have to explicitly subtract that from [pEl.offsetWidth]. Depending on your HTML structure that can be calculated.

    3) If the table columns are sized dynamically then changing the size of one element will cause the table to reflow in some browsers and you might get a stepped effect as you sequentially "fix" the input sizes. The solution is to assign specific widths to the column either as percentages or pixels OR collect the new widths and set them after. See the code below..

    var inputs = document.getElementsByTagName('input');
    var newSizes = [];
    for (var i = 0; i < inputs.length; i++)
    {
        var el = inputs[i];
        if (el.style.width == '100%')
        {
            var pEl = el.parentNode;  // Assumes the parent node is the TD...
            newSizes.push( { el: el, width: pEl.offsetWidth });
        }
    }
    
    // Set the sizes AFTER to avoid stepping...
    for (var i = 0; i < newSizes.length; i++)
    {
        newSizes[i].el.style.width = newSizes[i].width;
    }
    
    0 讨论(0)
  • 2021-01-29 21:10

    I solved the problem by using this

    tr td input[type=text] {
      width: 100%;
      box-sizing: border-box;
      -webkit-box-sizing:border-box;
      -moz-box-sizing: border-box;
      background:transparent !important;
      border: 0px;
    }
    
    0 讨论(0)
  • 2021-01-29 21:11

    Width value doesn't take into account border or padding:

    http://www.htmldog.com/reference/cssproperties/width/

    You get 2px of padding in each side, plus 1px of border in each side.
    100% + 2*(2px +1px) = 100% + 6px, which is more than the 100% child-content the parent td has.

    You have the option of:

    • Either setting box-sizing: border-box; as per @pricco's answer;
    • Or using 0 margin and padding (avoiding the extra size).
    0 讨论(0)
  • 2021-01-29 21:14

    The problem is due to the input element box model. I just recently found a nice solution to the issue when trying to keep my input at 100% for mobile devices.

    Wrap your input with another element, a div for example. Then apply the styling you want for your input to that the wrapper div. For example:

    <div class="input-wrapper">
     <input type="text" />
    </div>
    
    .input-wrapper {
        border-raius:5px;
        padding:10px;
    }
    .input-wrapper input[type=text] {
        width:100%;
        font-size:16px;
    }
    

    Give .input-wrapper rounded corner padding etc, whatever you want for your input, then give the input width 100%. You have your input padded nicely with a border etc but without the annoying overflow!

    0 讨论(0)
提交回复
热议问题