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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18714908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:18:49  来源:igfitidea点击:

Bootstrap 3 vertical form with one inline row of inputs

csstwitter-bootstrap

提问by gpcola

How should I restructure the following markup such that the second row's inputs are inline with the label for time between them? I thought I might be able to use 'form-inline' on a div but it doesn't appear to apply the correct styling.

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

http://bootply.com/80058

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>

What I want is to end up with

我想要的是结束

Take me to
<Address>

Date & Time
<Date> @ <Time>
Take me to
<Address>

Date & Time
<Date> @ <Time>

But in such as way as to have the date @ time row's inputs expand and contract responsively. It would be nice if they also acted like a col-md-n so that on smaller screens I end up with

但是,让日期@时间行的输入响应地扩展和收缩。如果他们也表现得像 col-md-n 那就太好了,这样我就可以在较小的屏幕上看到

Take me to
<Address>

Date & Time
<Date>
@
<Time>
Take me to
<Address>

Date & Time
<Date>
@
<Time>

采纳答案by Mike Oram

You are correct to think you can use form-inline

您认为可以使用表单内联是正确的

You want something like this:

你想要这样的东西:

<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>

回答by Pier-Luc Gendreau

The following solution works perfectly with Bootstrap 3:

以下解决方案与 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>

This will display first and last name side by side with labels above the fields.

这将并排显示名字和姓氏,并在字段上方带有标签。

回答by user3469676

I came up with this solution. Include the styles below and then format your form row as follows. You can leave out the unordered list, that's just what I needed for my site.

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

<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>

回答by Magmatic

Pier-Luc's solution has problems with vertical margin spacing when the screen is narrow or wide. This solution solves the margin problem:

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>