基础用法
<div class="input-group">
<span class="input-group-addon">@</span>
<input type="text" class="form-control">
</div>
addon元素
addon元素不仅可以是label和icon,还可以是复选框、单选框和按钮。
<span class="input-group-addon"><input type="checkbox"></span>
<span class="input-group-btn">
<button type="button" class="btn btn-default"></button>
</span>
也可以设置下拉菜单在addon元素里面,只要按钮设置好data-toggle即可
源码
.form-control {
display: block;
width: 100%;
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-control::-moz-placeholder,
.form-control:-ms-input-placeholder,
.form-control::-webkit-input-placeholder {
color: #999;
}
.input-group {
position: relative;
display: table;
border-collapse: separate;
}
.input-group .form-control {
float: left;
width: 100%;
margin-bottom: 0;
}
.input-group-addon,
.input-group-btn {
width: 1%; /* 设置最小值,方便表格模式进行等分*/
white-space: nowrap;
vertical-align: middle;
}
.input-group-btn,
.input-group-addon,
.input-group .form-control {
display: table-cell;
}
.input-group .form-control:first-child,
.input-group-addon:first-child,
.input-group-btn:first-child > .btn {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.input-group .form-control:last-child,
.input-group-addon:last-child,
.input-group-btn:last-child > .btn {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.input-group-addon {
padding: 6px 12px;
font-size: 14px;
font-weight: normal;
line-height: 1;
color: #555;
text-align: center;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 4px;
}
.input-group-btn {
position: relative;
font-size: 0;
white-space: nowrap;
}
.input-group-btn > .btn {
position: relative;
}
为了将各元素等高显示,设置了display: table-cell。还有对addon元素的圆角进行了处理,对于圆角处理不是简单的把第一个和最后一个元素的圆角设置了一下就行了,具体实现有点精妙,暂时放一边。。。