简体   繁体   English

输入从哪里获得额外的宽度?

[英]Where do inputs get their extra width from?

I am trying to create a button div and input to all be 80% the width of their container.我正在尝试创建一个按钮 div 并输入它们的容器宽度的 80%。

However the input is always bigger than 80%.但是输入总是大于 80%。 Why is this?为什么是这样? Do I need to use calc in some way to get it to 80%.我是否需要以某种方式使用 calc 才能使其达到 80%。

Even when setting the padding to 0px it is still bigger:即使将填充设置为 0px,它仍然更大:

https://jsfiddle.net/0rcv1ypb/ https://jsfiddle.net/0rcv1ypb/

<input/>
<button></button>
<div></div>


input{
  display: block;
  width: 80%;
}
button{
  display: block;
  width: 80%;
  height: 20px;
}
div{
  width: 80%;
  height: 20px;
  background: black;
}

As a side question: the default width for input appears to be 145px.作为一个附带问题:输入的默认宽度似乎是 145px。 Where does this value come from as it is not in the agent style sheet.这个值来自哪里,因为它不在代理样式表中。

Because the default browser styling is box-sizing: content-box .因为默认浏览器样式是box-sizing: content-box

Reset it and all elements get the same width as expected, even with different paddings/borders.重置它,所有元素都获得与预期相同的宽度,即使有不同的填充/边框。

 * { box-sizing: border-box; } input{ display: block; width: 80%; } button{ display: block; width: 80%; height: 20px; } div{ width: 80%; height: 20px; background: black; }
 <input/> <button></button> <div></div>

Take a look at the box-model and here .看看盒子模型这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM