Bootstrap 3垂直形式,带有一行内联输入

时间:2013-09-10 09:13:10

标签: css twitter-bootstrap

我应该如何重构以下标记,以便第二行的输入与它们之间的时间标签内联?我以为我可以在div上使用'form-inline',但似乎没有应用正确的样式。

http://bootply.com/80058

<div class="well">
    <form role="form">
        <div class="row">
            <fieldset>
                <div class="form-group">
                    <div class="col-md-12">
                        <label for="address">To take me to</label>
                        <input type="text" name="address" id="address" class="form-control" placeholder="Enter a location" required>
                    </div>
                </div>
            </fieldset>
        </div>
        <div class="row">
            <fieldset>
                <div class="form-group">
                    <div class="col-md-12">
                        <label for="date">Date &amp; Time</label>
                        <input type="text" name="date" id="date" placeholder="Enter a date" class="form-control" required>
                        <label for="time">@</label>
                        <input type="text" name="time" id="time" placeholder="Enter a time" class="form-control" required>
                    </div>
                </div>
            </fieldset>
        </div>
    </form>
</div>

我想要的是最终

Take me to
<Address>

Date & Time
<Date> @ <Time>

但是以这样的方式让日期@时间行的输入响应地扩展和收缩。如果他们也像col-md-n那样表现得很好,那么在较小的屏幕上,我最终会用

Take me to
<Address>

Date & Time
<Date>
@
<Time>

4 个答案:

答案 0 :(得分:21)

以下解决方案与Bootstrap 3完美配合:

<form>
    <div class="form-group">
        <div class="row">
            <div class="col-md-6">
                <label>First name</label>
                <input type="text" class="form-control">
            </div>
            <div class="col-md-6">
                <label>Last name</label>
                <input type="text" class="form-control">
            </div>
        </div>
    </div>
    <div class="form-group">
        <label>Address</label>
        <input type="text" class="form-control">
    </div>
</form>

这将在字段上方标记并排显示名字和姓氏。

答案 1 :(得分:10)

您认为可以使用form-inline

是正确的

你想要这样的东西:

<div class="row form-inline">
        <fieldset>
            <div class="col-md-12">
                <label for="date">Date &amp; Time</label>
            </div>
            <div class="form-group">
                <input type="text" required="" class="form-control" placeholder="Enter a date" id="date" name="date">
            </div>
            <div class="form-group">
                <label for="time">@</label>
            </div>
            <div class="form-group">
                <input type="text" required="" class="form-control" placeholder="Enter a time" id="time" name="time">                
            </div>
        </fieldset>
    </div>

答案 2 :(得分:0)

我提出了这个解决方案。包括下面的样式,然后格式化表单行,如下所示。您可以省略无序列表,这就是我的网站所需要的。

<div class="form-group">
    <label>Date &amp; Time</label>
    <ul class="list-unstyled">
        <li>
            <div class="input-group">
                <span class="input-group-input-addon"><input class="form-control input" type="text" style="width: 100px;" placeholder="Enter a date" /></span>
                <span class="input-group-addon">@</span>
                <input class="form-control" type="text" placeholder="Enter a time" />
            </div>
        </li>
    </ul>
    <a href="javascript:void(0);" class="btn btn-default btn-sm">
        <span><span class="glyphicon glyphicon-plus"></span> Add Material</span>
    </a>
</div>


<style>
@media (min-width: 768px) {
  .form-inline .input-group .input-group-input-addon{
    width: auto;
  }
}

.input-group > .input-group-input-addon > .input {
  border-radius: 6px;
}

.input-group-lg > .input-group-input-addon > .input {
  height: 46px;
  padding: 10px 16px;
  font-size: 18px;
  line-height: 1.33;
  border-radius: 6px;
}
select.input-group-lg > .input-group-input-addon > .input {
  height: 46px;
  line-height: 46px;
}
textarea.input-group-lg > .input-group-input-addon > .input,
select[multiple].input-group-lg > .input-group-input-addon > .input {
  height: auto;
}
.input-group-sm > .input-group-input-addon > .input {
  height: 30px;
  padding: 5px 10px;
  font-size: 12px;
  line-height: 1.5;
  border-radius: 3px;
}
select.input-group-sm > .input-group-input-addon > .input {
  height: 30px;
  line-height: 30px;
}
textarea.input-group-sm > .input-group-input-addon > .input,
select[multiple].input-group-sm > .input-group-input-addon > .input {
  height: auto;
}
.input-group-input-addon {
  display: table-cell;
}
.input-group-input-addon:not(:first-child):not(:last-child) > .input {
  border-radius: 0;
}
.input-group-input-addon {
  width: 1%;
  white-space: nowrap;
  vertical-align: middle;
}
.input-group-input-addon:first-child > .input {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.input-group-input-addon:last-child > .input {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}
.input-group-input-addon {
  position: relative;
  font-size: 0;
  white-space: nowrap;
}
.input-group-input-addon > .input {
  position: relative;
}
.input-group-input-addon > .input + .input {
  margin-left: -1px;
}
.input-group-input-addon > .input:focus,
.input-group-input-addon > .input:active,
.input-group > .form-control:focus,
.input-group > .form-control:active {
  z-index: 3;
}
.input-group-input-addon:not(:last-child) > .input {
  margin-right: -1px;
}
.input-group-input-addon:last-child > .input {
  margin-left: -1px;
}

@media (min-width: 768px) {
  .navbar-form .input-group .input-group-input-addon {
    width: auto;
  }
}
</style>

答案 3 :(得分:0)

当屏幕窄或宽时,Pier-Luc的解决方案存在垂直边距间距的问题。此解决方案解决了边距问题:

<form>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group" style="margin-bottom:15px">
                <label>First name</label>
                <input type="text" class="form-control">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group" style="margin-bottom:15px">
                <label>Last name</label>
                <input type="text" class="form-control">
            </div>
        </div>
    </div>
    <div class="form-group">
        <label>Address</label>
        <input type="text" class="form-control">
    </div>
</form>