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

前端 未结 14 1509
别那么骄傲
别那么骄傲 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:17

    You could use the CSS3 box-sizing property to include the external padding and border:

    input[type="text"] {
         width: 100%; 
         box-sizing: border-box;
         -webkit-box-sizing:border-box;
         -moz-box-sizing: border-box;
    }
    
    0 讨论(0)
  • 2021-01-29 21:18

    The problem with things like width:95%; is that they don't look right in wide flexible layouts (because 5% can then be like 30 pixels).

    The following solutions works very well for me (tested in Firefox, IE 9, Safari):

    <table style="background-color: blue; width: 100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td style="background-color: red;   padding: 3px;">
            <div style="margin-right: 3px;">
                <div style="padding-right: 3px;">
                    <input type="text" style="width:100%;">
                </div>
            </div>
        </td>
        <td style="background-color: purple;   padding: 3px;">
            <div style="margin-right: 3px;">
                <div style="padding-right: 3px;">
                    <input type="text" style="width:100%;">
                </div>
            </div>
        </td>
        <td style="background-color: green;   padding: 3px;">
            <div style="margin-right: 3px;">
                <div style="padding-right: 3px;">
                    <input type="text" style="width:100%;">
                </div>
            </div>
        </td>
    </tr>
    </table>
    

    (added the colors to make it easier to notice the correct padding)

    The trick is to wrap the input with two divs, the outer has a 3px margin (which makes it 3px smaller than the td), the inner has a 3px padding. This makes the input 6px smaller than the td, which is what you wanted to begin with.

    I am not sure about the mechanics of this, but it works.

    In this simple example, it also works with just a single div with right margin 6. However, in the case of my web application, only the above solution works.

    <table style="background-color: blue; width: 100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td style="background-color: red;   padding: 3px;">
            <div style="margin-right: 6px;">
                    <input type="text" style="width:100%;">
            </div>
        </td>
        <td style="background-color: purple;   padding: 3px;">
            <div style="margin-right: 6px;">
                    <input type="text" style="width:100%;">
            </div>
        </td>
        <td style="background-color: green;   padding: 3px;">
            <div style="margin-right: 6px;">
                    <input type="text" style="width:100%;">
            </div>
        </td>
    </tr>
    </table>
    
    0 讨论(0)
提交回复
热议问题