2014-02-27 43 views
3

我有一个问题,在容器内设置width: 100%输入的内容超过了容器的容量。这个问题似乎并不虽然带有按钮的情况发生:输入溢出容器

<form>  
    <input type="text" class="input"></input>  
    <button>Button</button> 
</form> 

CSS:

form { 
    max-width: 200px; 
    border: 1px solid #eee; 
} 

.input, button { 
    width: 100%; 
} 

在这个例子中,按钮正确填充容器,但是输入进一步扩展了一下:

enter image description here

我该如何解决这个问题?

我创建了一个codepen:http://codepen.io/jviotti/pen/qfFmH

回答

3

可以解决这个问题将

.input, button { 
    width: 100%; 
    box-sizing: border-box; /* add this */ 
     -webkit-box-sizing: border-box; 
     -moz-box-sizing: border-box; 
} 
+1

http://codepen.io/Paulie-D/pen/vflcL –

+0

@Paulie_D THX为codepen,和供应商前缀建议,我更新了我的答案,但我宁愿将它设置在'.input,button'而不是'*'女巫可能会有一些性能问题。 –

+0

您提到的性能问题一直是一些讨论的主题,并且发现不是重要因素。 –