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

前端 未结 14 1507
别那么骄傲
别那么骄傲 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 20:54

    I usually set the width of my inputs to 99% to fix this:

    input {
        width: 99%;
    }
    

    You can also remove the default styles, but that will make it less obvious that it is a text box. However, I will show the code for that anyway:

    input {
        width: 100%;
        border-width: 0;
        margin: 0;
        padding: 0;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
    }
    

    Ad@m

    0 讨论(0)
  • 2021-01-29 20:59

    instead of this margin struggle just do

    input{
        width:100%;
        background:transparent !important;
    }
    

    this way the cell border is visible and you can control row background color

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

    Take out the "http://www.w3.org/TR/html4/loose.dtd" from the DOCTYPE declaration and it fix your problem.

    Cheers! :)

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

    I know that this question is 3 years old but the problem still persists for current browsers and folks are still coming here. The solution for now is calc():

    input {
        width: calc(100% - 12px); /* IE 9,10 , Chrome, Firefox */
        width: -webkit-calc(100% - 12px); /*For safari 6.0*/
    }
    

    It is supported by most modern browsers.

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

    I solved the problem by applying box-sizing:border-box; to the table cells themselves, besides doing the same with the input and the wrapper.

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

    The problem lies in border-width of input element. Shortly, try setting the margin-left of the input element to -2px.

    table input {
      margin-left: -2px;
    }
    
    0 讨论(0)
提交回复
热议问题