What is it in the CSS/DOM that prevents an input box with display: block from expanding to the size of its container

前端 未结 5 1991
离开以前
离开以前 2020-11-30 10:29

Sample HTML/CSS:

div.container
5条回答
  •  一向
    一向 (楼主)
    2020-11-30 10:52

    You can accomplish this by making use of the box-sizing CSS property on the input box:

    input {
        display: block;
        width: 100%;
        box-sizing: border-box; /* CSS3 */
        -moz-box-sizing: border-box; /* Firefox */
        -ms-box-sizing: border-box; /* IE8 */
        -webkit-box-sizing: border-box; /* Safari */
        -khtml-box-sizing: border-box; /* Konqueror */
    }
    

    Additionally, to get it to work with IE6 & IE7, you'll need to throw this in your HTML:

    
    

    See it in action, here.

    Get the boxsizing.htc here.

    SpliFF's post was my source a while back and I've used it a couple times since.

提交回复
热议问题