在Twitter Bootstrap 2.3.2中使用input-block-level和input-prepend

时间:2013-08-24 11:19:11

标签: twitter-bootstrap twitter-bootstrap-2

我想知道如何使用twitter bootstrap 2.3.2中的输入前置图标和响应式布局来使输入字段填充可用宽度?

bootstrap 输入块级别中的css样式可以正常工作,直到您在输入前添加图标...如何使用prependet输入获得相同的功能?

<div class="control-group">
  <label class="control-label" for="inputIcon">Email address</label>
  <div class="controls">
    <div class="input-prepend ">
      <span class="add-on"><i class="icon-envelope"></i></span>
      <input class="span2" id="inputIcon" type="text" class="input-block-level">
    </div>
  </div>
</div>
<div class="control-group">
    <label class="control-label" for="inputPassword">Password</label>
    <div class="controls">
      <input type="password" id="inputPassword" placeholder="Password" class="input-block-level">
    </div>
</div>

以下是说明此问题的代码示例:http://jsfiddle.net/r3CyL/

1 个答案:

答案 0 :(得分:8)

您需要添加一些额外的CSS

DEMO jsFiddle

.input-prepend.input-block-level {
  display: table; 
}
.input-prepend.input-block-level .add-on {
  display: table-cell;
}
.input-prepend.input-block-level > input {
  width: 100%;
}

您可以将border颜色添加到input-prepend.input-block-level .add-on {以改善它的外观。

input-prepend.input-block-level .add-on {
    display: table-cell;
    border-right: #fff;
}

<强> EDITED

另一种更为复杂的做法。这也是Bootstrap的批准。

DEMO jsFiddle

.input-append.input-block-level,
.input-prepend.input-block-level {
  display: table;
}

.input-append.input-block-level .add-on,
.input-prepend.input-block-level .add-on {
  display: table-cell;
  width: 1%; /* remove this if you want default bootstrap button width */
}

.input-append.input-block-level > input,
.input-prepend.input-block-level > input {
  box-sizing: border-box; /* use bootstrap mixin or include vendor variants */
  display: table; /* table-cell is not working well in Chrome for small widths */
  min-height: inherit;
  width: 100%;
}

.input-append.input-block-level > input {
  border-right: 0;
}

.input-prepend.input-block-level > input {
  border-left: 0;
}