如何使Bootstrap输入框的宽度与其父元素完全相同?
正如steve-obrien在 Bootstrap Issue #1058中所写:
将宽度设置为100%时,直接应用于输入框时会因为没有考虑填充而不起作用。因此,您最终会得到容器的100%加上输入框上的填充,因此输入框通常会超出其容器。
该问题提供了各种解决方案,但我正在寻找最佳方法--最好是由Bootstrap提供的CSS类。
如何使Bootstrap输入框的宽度与其父元素完全相同?
正如steve-obrien在 Bootstrap Issue #1058中所写:
将宽度设置为100%时,直接应用于输入框时会因为没有考虑填充而不起作用。因此,您最终会得到容器的100%加上输入框上的填充,因此输入框通常会超出其容器。
该问题提供了各种解决方案,但我正在寻找最佳方法--最好是由Bootstrap提供的CSS类。
input-block-level
类对我来说在各种屏幕宽度下都适用得很好。它由Bootstrap中的mixins.less
定义如下:
// Block level inputs
.input-block-level {
display: block;
width: 100%;
min-height: 28px; // Make inputs at least the height of their button counterpart
.box-sizing(border-box); // Makes inputs behave like true block-level elements
}
这与“assembler”在他在问题#1058上的评论中建议的样式非常相似。
只需要添加box-sizing属性:
input[type="text"] {
box-sizing: border-box;
}
{ width: 100%; box-sizing: border-box; }
对我起作用了。 - aalaap如果您使用 C# ASP.NET MVC 的默认模板,您可能会发现 site.css 覆盖了一些 Bootstrap 样式。 如果您想像我一样使用 Bootstrap,让 Microsoft 不知不觉地覆盖这些样式可能会带来很大的挫败感! 随意删除任何不需要的样式...
/* Set width on the form input elements since they're 100% wide by default */
input,
select,
textarea {
max-width: 280px;
}
For anyone Googling this, one suggestion is to remove all the input-group
class instances. Worked for me in a similar situation. Original code:
<form>
<div class="bs-callout">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="time" placeholder="Time">
</div>
</div>
</div>
<div class="col-md-8">
<div class="form-group">
<div class="input-group">
<select name="dtarea" class="form-control">
<option value="1">Option value 1</option>
</select>
</div>
</div>
</div>
</div>
<div class="row">
<div class="input-group">
<input type="text" name="reason" class="form-control" placeholder="Reason">
</div>
</div>
</div>
</form>
New code:
<form>
<div class="bs-callout">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" name="time" placeholder="Time">
</div>
</div>
<div class="col-md-8">
<div class="form-group">
<select name="dtarea" class="form-control">
<option value="1">Option value 1</option>
</select>
</div>
</div>
</div>
<div class="row">
<input type="text" name="reason" class="form-control" placeholder="Reason">
</div>
</div>
</form>
这是CSS标准指定的初始和默认值。宽度和高度属性仅包括内容,但不包括填充、边框或外边距。
宽度和高度属性包括内容、填充和边框,但不包括外边距。
参考资料:https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing
该 CSS 的兼容性良好。
.container-fluid
。怎么样?
input[type="text"] {
max-width:none;
}
input,select,textarea {
max-width: 280px;}
只需添加:
width: 100% !important;
form-control
,它会添加width: 100%
。 - Courtney Christensenmax-width:280px
。实际上,不用管它,这是微软在新的ASP.NET项目中默认包含的Site.css文件。噢! - Finster