【问题标题】:Vertically align two columns input fields one with labels and other without label垂直对齐两列输入字段,一列带标签,另一列不带标签
【发布时间】:2018-07-15 22:27:18
【问题描述】:

我正在制作发票申请表。现在我在一行中做了五列。每列都有输入字段。第四列和第五列有四个输入字段,但第五列字段没有标签。现在我需要的是使用引导类对齐四列和第五列,而无需任何自定义 CSS。因为我不需要第五列中的标签。花了几个小时后,我不知道如何对齐两列。

HTML:

      <div class="row">
               <div class="col-sm-3>
                 <div class="form-group"> 
                  <label for="inputEmail">field</label> 
                  <input type="text" class="form-control" id="" 
                     placeholder=""> </div>     
               </div>
               <div class="col-sm-3>
               <!--i am using these four lines code in every col for input fields for sake clarity i wrote it only here.
                  <div class="form-group"> 
                  <label for="inputEmail">field</label> 
                  <input type="text" class="form-control"id="" 
                     placeholder="item desc"> </div>     
               </div>
               <div class="col-sm-3>
                
               </div>
               <!---fourth col-!>
               <div class="col-sm-3>
               </div>
               <!--fifth col--!>
               <div class="col-sm-3>
               <!-I need to align this column with the above column remeber this coloumn field does not have labels thats why I am not able to align both columns(fourth and fifth)-!>
               </div>

            </div>

【问题讨论】:

    标签: html bootstrap-4


    【解决方案1】:

    首先,我不知道你如何用col-sm-3 使一行5 列。这只会使一行中出现 4 列,并且将第五列推送到新行。

    如果您的第 4 列和第 5 列都有 4 个输入,但只有第 5 列输入没有标签。为什么不将第 4 列和第 5 列合并为一列并使用行作为输入?

    <div class="container">
      <div class="row">
        <div class="col-sm-2 offset-sm-1">
          <div class="form-group">
            <label>Field</label>
            <input type="text" class="form-control" />
          </div>
        </div>
        <div class="col-sm-2">
          <div class="form-group">
            <label>Field</label>
            <input type="text" class="form-control" />
          </div>
        </div>
        <div class="col-sm-2">
          <div class="form-group">
            <label>Field</label>
            <input type="text" class="form-control" />
          </div>
        </div>
        <div class="col-sm-4">
          <div class="form-group">
            <label>Field 1</label>
            <div class="row">
              <div class="col-sm-6">
                <input type="text" class="form-control" />
              </div>
              <div class="col-sm-6">
                <input type="text" class="form-control" placeholder="input 1 without label" />
              </div>
            </div>
          </div>
          <div class="form-group">
            <label>Field 2</label>
            <div class="row">
              <div class="col-sm-6">
                <input type="text" class="form-control" />
              </div>
              <div class="col-sm-6">
                <input type="text" class="form-control" placeholder="input 2 without label" />
              </div>
            </div>
          </div>
          <div class="form-group">
            <label>Field 2</label>
            <div class="row">
              <div class="col-sm-6">
                <input type="text" class="form-control" />
              </div>
              <div class="col-sm-6">
                <input type="text" class="form-control" placeholder="input 3 without label" />
              </div>
            </div>
          </div>
          <div class="form-group">
            <label>Field 2</label>
            <div class="row">
              <div class="col-sm-6">
                <input type="text" class="form-control" />
              </div>
              <div class="col-sm-6">
                <input type="text" class="form-control" placeholder="input 4 without label" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    

    小提琴: https://jsfiddle.net/aq9Laaew/91237/

    【讨论】:

    • 第 4 和第 5 个输入字段不应位于第 1 到第 3 列之下。
    • 什么?我不明白你的意思。为了简单起见,我为每一列复制了字段标签和输入。这只是您输入字段的占位符。
    • 这正是我正在寻找的。我要查看学习目的。还有一个问题请耐心等待。我需要在第一行字段下放置一个按钮,该按钮应该创建第一行字段。我应该怎么做?
    • @stackOverFlowGeek:您可以使用&lt;table&gt; 来解决此问题的结构,但在列将成为自己的行的较小屏幕上,您将失去响应能力。
    • 我不明白。你说第五列没有标签。但是你现在需要那个标签吗?
    【解决方案2】:

    使用mt-2form-group,其中label 为空。

    在此处了解 bootstrap-4 间距:https://getbootstrap.com/docs/4.0/utilities/spacing/

      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    <div class="row">
                   <div class="col-sm-2">
                     <div class="form-group"> 
                      <label for="inputEmail">field</label> 
                      <input type="text" class="form-control" id="" 
                         placeholder=""> </div>     
                   </div>
                   <div class="col-sm-2">
                      <div class="form-group"> 
                      <label for="inputEmail">field</label> 
                      <input type="text" class="form-control"id="" 
                         placeholder="item desc"> </div>     
                   </div>
                     <div class="col-sm-2">
                     <div class="form-group"> 
                      <label for="inputEmail">field</label> 
                      <input type="text" class="form-control" id="" 
                         placeholder=""> </div>     
                   </div>
                   <div class="col-sm-2">
                      <div class="form-group  mt-2"> 
                      <label for="inputEmail "></label> 
                      <input type="text" class="form-control"id="" 
                         placeholder="item desc" > </div>     
                   </div>
                     <div class="col-sm-2">
                     <div class="form-group"> 
                      <label for="inputEmail">field</label> 
                      <input type="text" class="form-control" id="" 
                         placeholder=""> </div>     
                   </div>
    
                    
    </div>

    【讨论】:

    • 告诉我更多帮助
    • tnx 回答你的第五列和第四列如果我输入更多字段,则不对齐。
    • 我的意思是第 4 和第 5 个输入字段不应位于第 1 到第 3 列之下。
    • 它仅作为示例,您可以将其删除....如果您标记为答案并投赞成票,真的很适用
    • 您的回答并不完全有帮助,但对这项工作很有帮助。我只是赞成你的回答
    猜你喜欢
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2016-10-08
    • 2023-01-03
    • 1970-01-01
    相关资源
    最近更新 更多