如何垂直对齐文本字段及其标签和按钮

How to vertically align text field with it's label and button

我基本上是在尝试这种布局:

标签文本字段按钮

例如: 份数[5][添加]

原以为这会很简单。但我现在已经挣扎了几个小时,这是我最接近的一次,这绝对是可怕的:

这是我的代码:

<div class="row col-md-12">
            <form class="form-inline pull-right">
                <div class="form-group">
                    <label for="numberOfCopies">Number of copies</label>
                    <input size="1" type="text" class="form-control" id="numberOfCopies">
                    <button type="submit"  class="btn btn-primary">Add</button>
                </div>
            </form>
        </div>

起初有些Bootstrap-Basics:

  • 你的 'row-div' 应该被 'container-div'
  • 包裹起来
  • rowcol-xx-x 类 需要单独 div-tags

正如您在下面的示例中看到的,您可以使用 css.

轻松完成此操作
  • 使用 display: flex; 我们可以并排获得 form-group 的所有元素。
  • 使用 white-space: nowrap 我们 'tell' 标签仅显示为 one-line。
  • 使用 margin 标签,我们会将元素放到正确的位置。

To learn more take a look at the documentations of display, white-space and margin.

I think the usage of input-groups could be interesting, too. Feel free to ask per comment, if anything is unclear.

.form-group.flex {
  display: flex;
}

.form-group.flex label {
  white-space: nowrap;
  margin-top: 7px;
}

.form-group.flex input {
  margin: 0 7px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div class="container">
  <div class="row">
    <div class="col-md-12">
      <form class="form-inline pull-right">
        <div class="form-group flex">
          <label for="numberOfCopies">Number of copies</label>
          <input size="1" type="text" class="form-control" id="numberOfCopies">
          <button type="submit" class="btn btn-primary">Add</button>
        </div>
      </form>
    </div>
  </div>
</div>