如何在水平表单上以内联格式显示Bootstrap输入?

时间:2015-11-22 09:02:24

标签: twitter-bootstrap

我希望在大屏幕中显示内联中的某些表单元素,并在屏幕较小时显示相同的垂直元素。如何做到这一点?请帮助。请告诉我以获取更多信息。谢谢

enter image description here

此单个html文件中的所有代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</head>
<body>

<div class="container">
  <h2>Horizontal form</h2>
  <form class="form-horizontal" role="form"  style="border: 1px solid;  padding: 10px">
    <div class="form-group">
      <label class="control-label col-sm-2" for="email">Email:</label>
      <div class="col-sm-5">
        <input type="email" class="form-control" id="email" placeholder="Enter email">
      </div>
    </div>



    <div class="form-group ">
      <label class="control-label col-sm-2" for="pwd">Password:</label>
      <div class="col-sm-5">          
        <input type="password" class="form-control" id="pwd" placeholder="Enter password">
      </div>
    </div>




    <div class="form-group" >
      <label class="control-label col-sm-2" for="designation">Designation:</label>

      <div class="col-sm-2">

        <select class="form-control  col-xs-2"   id="designation" onchange="updateText('designation')" >
        <option value="" disabled selected>Select Designation</option>          
        <option value="04">Chairman</option>                            
        <option value="03">Dean</option>
        </select>       

        <label type="text" class="control-label " for="designationText" >Designation Code </label>              
        <input type="text" value=""  class="form-control" id="designationText" placeholder="Code" />      

      </div>


    </div> 








    <div class="form-group">        
      <div class="col-sm-offset-2 col-sm-10">
        <div class="checkbox">
          <label><input type="checkbox"> Remember me</label>
        </div>
      </div>
    </div>
    <div class="form-group">        
      <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-default">Submit</button>
      </div>
    </div>
  </form>
</div>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

您可以使用网格来执行此操作,确保您的表单首先在其上有内联类,然后按照这些行执行某些操作:

http://jsfiddle.net/oL2rv8pu/4/

<div class="container">
    <h2>Horizontal form</h2>
    <form class="form-horizontal" role="form"  style="border: 1px solid;  padding: 10px">
        <div class="form-group">
            <label class="control-label col-sm-2" for="email">Email:</label>
            <div class="col-sm-5">
                <input type="email" class="form-control" id="email" placeholder="Enter email">
            </div>
        </div>
        <div class="form-group ">
            <label class="control-label col-sm-2" for="pwd">Password:</label>
            <div class="col-sm-5">          
                <input type="password" class="form-control" id="pwd" placeholder="Enter password">
            </div>
        </div>
        <div class="form-group row">
            <label class="control-label col-sm-2" for="designation">Designation:</label>
            <div class="col-sm-2">
                <select class="form-control" id="designation" onchange="updateText('designation')" >
                    <option value="" disabled selected>Select Designation</option>
                    <option value="04">Chairman</option>
                    <option value="03">Dean</option>
                </select>    
            </div>
            <label type="text" class="control-label col-sm-2" for="designationText" >Designation Code </label>                       <div class="col-sm-2">
                <input type="text" value="" class="form-control col-sm-2" id="designationText" placeholder="Code" /> 
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <div class="checkbox">
                    <label><input type="checkbox"> Remember me</label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-default">Submit</button>
            </div>
        </div>
    </form>
</div>

您可能需要扩大列大小以使所有内容更好地适应,我建议将每行等同于偶数(例如2 + 6而不是2 + 5),这样您就可以将它们分成四个适当的大屏幕上的列(2 + 2 + 2 + 2)。就目前而言,它将比您页面的其他部分宽一列。

答案 1 :(得分:0)

您需要取消嵌套来自designationText的{​​{1}}标签/输入,并将它们放在自己的列中,但仍然位于与designation相同的表单组中。然后应用适当数量的列。

*注意:因为这些标签/输入位于单个designation中,所以它们与其他标签/输入之间的余量不同。您可以在媒体查询中添加一些CSS来修复此差异。

请参阅示例代码段和Docs

form-group
.myForm {
  border: 1px solid;
  padding: 10px
}
@media (max-width: 767px) {
  #form-group-top {
    margin-top: 15px;
  }
}