在移动设备上将 Bootstrap 布局从内联表单更改为网格

Posted

技术标签:

【中文标题】在移动设备上将 Bootstrap 布局从内联表单更改为网格【英文标题】:Change Bootstrap layout from inline form to grid on mobile device 【发布时间】:2016-08-21 22:02:39 【问题描述】:

我有一个复杂的 Web 表单,其中一行包含许多字段。 Bootstrap 内联表单在大屏幕上效果很好。但是,要求是使页面具有响应性,以便内联表单在平板电脑和手机等较小的屏幕上转换为 3 列网格布局。

大屏内联表单:

手机屏幕上的必需布局

有可能吗?这是针对 Bootstrap 3 的。

非常感谢任何提示!

代码片段:

<div class="container">

<form class="form-horizontal" data-toggle="validator" role="form" action="" method="post" id="form1">


    <fieldset style="background-color:#F7F7F7">

        <div class="row">
          <div class="form-inline" style="text-align:left">

            <div class="form-group"   style="margin-right:30px;">
                <div class="floating-label-form-group" style="top:-5px;width:180px">
                    <label class="control-label" for="Selection1">Selection1</label>
                    <select id="Selection1" name="Selection1" class="form-control relationship selectpicker" required>
                        <option value="" disabled selected>Selection1 *</option>
                        <option value="1">option 1</option>
                        <option value="2">option 2/option>
                        <option value="3">option 3</option>
                    </select>
                </div>
                <span class="help-block with-errors"></span>
            </div>

            <div class="form-group ">
                <div class="floating-label-form-group">
                    <label class="control-label" for="fName">1st Name</label>  
                    <input id="fName" name="fName" type="text" required>
                </div>
                <span class="help-block with-errors"></span>
            </div>
            <div class="form-group "  style="margin-right:30px">
                <div class="floating-label-form-group">
                    <label class="control-label" for="lName">Last Name</label>  
                    <input id="lName" name="lName" type="text" required>
                </div>
                <span class="help-block with-errors"></span>
            </div>

            <div class="form-group">
                <div class="floating-label-form-group" style="top:-5px;width:120px">
                    <label class="control-label" for="gender">Gender</label>
                    <select id="gender" name="gender" class="form-control selectpicker" required>
                        <option value="" disabled selected>gender *</option>
                        <option value="1">M</option>
                        <option value="2">F</option>
                    </select>
                </div>
                <span class="help-block with-errors"></span>
            </div>

            <div class="form-group">
                <div class="floating-label-form-group" style="top:-5px;width:160px">
                    <label class="control-label" for="selection2">Selection2</label>
                    <select id="selection2" name="selection2" class="form-control selectpicker" required>
                        <option value="" disabled selected>Selection2 *</option>
                        <option value="1">option 1</option>
                        <option value="2">option 2</option>
                    </select>
                </div>
                <span class="help-block with-errors"></span>
            </div>

          </div>
        </div><!-- /row -->

    </fieldset>
</form>

【问题讨论】:

我什么都试过了。 SO一直是最后的手段。我不是要求有人为我编码,为了记录。我相信其他一些人可能面临类似的情况。如果@Paulie_D 无法提供有关如何解决问题的任何提示,请不要回来投反对票。 只尝试显示顶部屏幕截图的内联表单布局,因此我故意模糊了文本。如果有人发现它令人困惑,我深表歉意。应该使用whiteout。是否必须给出代码 sn-p 才能理解问题? 相同的结果...我们不知道什么元素是什么,以及它们应该如何定位在之前/之后。这就是为什么我们要求您使用代码进行演示...这样我们就可以看到您进行更改时发生的情况。 添加了代码片段。谢谢你。屏幕截图将被编辑以反映代码。 你能分享你的 CSS 吗?您正在使用一些自定义类。 【参考方案1】:

试试这个。使用 col-md-push-* 和 col-md-pull-*

更新 JSFIDDLE

这里在form-group类和样式之前使用了form-inline类

.form-inline .form-control     
    width: 100%;   

.nopadding 
   padding: 0px 2px 0px 0px !important;
   margin: 0px !important;
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
 <div class="form-inline">
 <div class="form-group">
             <div class="row">
             <div class="col-md-4 col-md-push-8">
                     <div class="col-xs-6 nopadding">
                     <label class="control-label" for="lName">Gender</label>  
                    <select id="gender" name="gender" class="form-control selectpicker" required>
                        <option value="" disabled selected>gender *</option>
                        <option value="1">M</option>
                        <option value="2">F</option>
                    </select>
                    </div>
                
                <div class="col-xs-6 nopadding">
                    <label class="control-label" for="selection2">Selection2</label>
                    <select id="selection2" name="selection2" class="form-control selectpicker" required>
                        <option value="" disabled selected>Selection2 *</option>
                        <option value="1">option 1</option>
                        <option value="2">option 2</option>
                    </select>
                    </div></div>
                     <div class="col-md-4">
                <div class="col-xs-6 nopadding">
                    <div class="floating-label-form-group">
                    <label class="control-label" for="fName">1st Name</label>  
                    <input class="form-control" id="fName" name="fName" type="text" required>
                </div>
                </div>
                <div class="col-xs-6 nopadding">
                    
                    <label class="control-label" for="lName">Last Name</label>  
                    <input class="form-control" id="lName" name="lName" type="text" required>
                    </div></div>
  <div class="col-md-4 col-md-pull-8">
                <div class="col-xs-12 nopadding">
                    <label class="control-label" for="Selection1">Selection1</label>
                    <select id="Selection1" name="Selection1" class="form-control relationship selectpicker" required>
                        <option value="" disabled selected>Selection1 *</option>
                        <option value="1">option 1</option>
                        <option value="2">option 2</option>
                        <option value="3">option 3</option>
                    </select>
                </div>
            
             </div>
        </div>
  </div>
</div>

【讨论】:

不是这样的。请在发布解决方案之前阅读问题。原始布局是 INLINE。 Chensformers,我更新了我的代码..请查看我上面的片段 在属性中使用 0 时省略单位。 0px, 0pt, 0%, 0em, 0rem 等等。所有这些都给出了相同的结果,即 0。【参考方案2】:

我修改了您的一些 html 标记,删除了一些内联样式并添加了一些类。

这就是您要查找的内容:

.myform 
  margin-top: 10px;

.myform fieldset 
  padding: 10px 20px;
  background-color: #F7F7F7;

.myform .form-group 
  display: block;

.myform input,
.myform select 
  min-width: 100%;
  border-radius: 0;

.myform .double-input 
  margin: 0;

.myform .double-input div[class^='col-'],
.myform .double-input div[class*=' col-'] 
  padding-left: 0;

.myform .double-input:first-child div[class^='col-'],
.myform .double-input:first-child div[class*=' col-'] 
  padding: 0;

@media (min-width: 992px) 
  .control-label 
    text-align: left;
    display: block;
  
  .myform .double-input:first-child div[class^='col-'],
  .myform .double-input:first-child div[class*=' col-'] 
    padding-right: 3px;
  

@media (max-width: 992px) 
  .myform input,
  .myform select 
    border-top: 0;
    border-right: 0;
    border-left: 0;
  
  #fName 
    border-right: 1px solid #ccc;
  
  #gender 
    border-right: 1px solid #ccc;
  
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">

  <form class="myform" data-toggle="validator" role="form" action="" method="post" id="form1">


    <fieldset>

      <div class="row">
        <div class="col-md-2">
          <div class="form-group">
            <div class="floating-label-form-group">
              <label class="control-label" for="Selection1">Selection1</label>
              <select id="Selection1" name="Selection1" class="form-control relationship selectpicker" required>
                <option value="" disabled selected>Selection1 *</option>
                <option value="1">option 1</option>
                <option value="2">option 2/option>
                  <option value="3">option 3</option>
              </select>
            </div>
            <span class="help-block with-errors"></span>
          </div>
        </div>
        <div class="col-md-5">
          <div class="row double-input">
            <div class="col-xs-6">
              <div class="form-group">
                <div class="floating-label-form-group">
                  <label class="control-label" for="fName">1st Name</label>
                  <input id="fName" class="form-control" name="fName" type="text" required>
                </div>
                <span class="help-block with-errors"></span>
              </div>
            </div>
            <div class="col-xs-6">
              <div class="form-group ">
                <div class="floating-label-form-group">
                  <label class="control-label" for="lName">Last Name</label>
                  <input id="lName" class="form-control" name="lName" type="text" required>
                </div>
                <span class="help-block with-errors"></span>
              </div>
            </div>
          </div>
        </div>
        <div class="col-md-5">
          <div class="row double-input">
            <div class="col-xs-6">
              <div class="form-group">
                <div class="floating-label-form-group">
                  <label class="control-label" for="gender">Gender</label>
                  <select id="gender" name="gender" class="form-control selectpicker" required>
                    <option value="" disabled selected>gender *</option>
                    <option value="1">M</option>
                    <option value="2">F</option>
                  </select>
                </div>
                <span class="help-block with-errors"></span>
              </div>
            </div>
            <div class="col-xs-6">
              <div class="form-group">
                <div class="floating-label-form-group">
                  <label class="control-label" for="selection2">Selection2</label>
                  <select id="selection2" name="selection2" class="form-control selectpicker" required>
                    <option value="" disabled selected>Selection2 *</option>
                    <option value="1">option 1</option>
                    <option value="2">option 2</option>
                  </select>
                </div>
                <span class="help-block with-errors"></span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- /row -->
    </fieldset>
  </form>

【讨论】:

【参考方案3】:

        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
        <div class="container-fluid">

        <form class="form-horizontal" data-toggle="validator" role="form" action="" method="post" id="form1">


            <fieldset style="background-color:#F7F7F7" style="width:100%;">
                <div class="row">
                  <div class="form-inline" style="text-align:left">
         <div class="form-group col-xs-12 col-sm-4 col-sm-push-8">
                        <div class="floating-label-form-group col-xs-6 no-pad-right">
                            <label class="control-label" for="gender">Gender</label>
                            <select id="gender" name="gender" class="form-control selectpicker" required>
                                <option value="" disabled selected>gender *</option>
                                <option value="1">M</option>
                                <option value="2">F</option>
                            </select>
                            <span class="help-block with-errors">Gender helper text</span>
                        </div>             
                        <div class="floating-label-form-group col-xs-6 no-pad-right">
                            <label class="control-label" for="selection2">Selection2</label>
                            <select id="selection2" name="selection2" class="form-control selectpicker" required>
                                <option value="" disabled selected>Selection2 *</option>
                                <option value="1">option 1</option>
                                <option value="2">option 2</option>
                            </select>
                            <span class="help-block with-errors">Selection2 helper text</span>
                        </div>              
                    </div>
                    <div class="form-group col-xs-12 col-sm-3 col-sm-pull-3">
                        <div class="floating-label-form-group col-md-12 no-pad-right">
                            <label class="control-label" for="Selection1">Selection1</label>
                            <select id="Selection1" name="Selection1" class="form-control relationship selectpicker" required>
                                <option value="" disabled selected>Selection1 *</option>
                                <option value="1">option 1</option>
                                <option value="2">option 2/option>
                                <option value="3">option 3</option>
                            </select>
                            <span class="help-block with-errors">Selection1 helper text</span>
                        </div>               
                    </div>

                    <div class="form-group col-xs-12 col-sm-5 col-sm-pull-3">
                        <div class="floating-label-form-group col-xs-6 no-pad-right">
                            <label class="control-label" for="fName">1st Name</label>  
                            <input id="fName" name="fName" class="form-control" type="text" required>
                            <span class="help-block with-errors">1st Name helper text</span>
                        </div>
                        

                        <div class="floating-label-form-group col-xs-6 no-pad-right">
                            <label class="control-label" for="lName">Last Name</label>  
                            <input id="lName" name="lName" class="form-control" type="text" required>
                            <span class="help-block">Last Name helper text</span>
                        </div>
                        
                    </div>
                    
                  </div>
                </div><!-- /row -->
            </fieldset>
        </form>

【讨论】:

以上是关于在移动设备上将 Bootstrap 布局从内联表单更改为网格的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap/flexbox 卡:在桌面上将图像移动到左侧/右侧

如何在移动设备上将现有按钮固定到底部? (引导)

Django Crispy Forms 重新排列布局并保留 Bootstrap 内联格式?

Bootstrap 表单

一步一步学习Bootstrap系列--表单布局

在移动屏幕尺寸上将按钮更改为“btn-block”