input text box width same as parent

前端 未结 3 940
走了就别回头了
走了就别回头了 2021-01-18 05:01

How can I have a textbox that inherits the width of it\'s container?

For example I have a div that has a textbox inside it.

相关标签:
3条回答
  • 2021-01-18 05:24

    Just add style='width:100%;' (or how much % do you need) to the Input

      <div id='content' style='width:100%;'> <input style='width:100%;' type='text' name='txtUsername'> </div>
    
    0 讨论(0)
  • 2021-01-18 05:29

    Have you tried setting the width of the input to 100%? e.g.

    input {width:100%;}
    
    0 讨论(0)
  • 2021-01-18 05:41

    You should use box-sizing:border-box together with width:100%. This way input will fit 100% of container's width but will not overflow it because of added input's padding:

    #content input
    {
        width:100%;
        box-sizing:border-box;
        -moz-box-sizing:border-box;
    }
    

    Live demo: http://jsfiddle.net/745Mt/1/

    More information about box-sizing CSS property with examples: http://css-tricks.com/box-sizing/

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