在单行中对齐表单元素

时间:2018-08-28 20:25:57

标签: html css twitter-bootstrap

我正在尝试将页面中的html元素单行对齐。

演示: http://plnkr.co/edit/ulLyUxj9C9PxxvpTnm3N?p=preview

在演示插件中,单击右侧的“在单独的窗口中启动窗口”图标时,将打开一个单独的窗口,其中显示表单元素(未单行对齐)。 我希望两个文本元素和“提交”按钮位于同一行,但是使用演示代码,提交和重置按钮与 文本字段。 下面是示例代码:

<div class="col-sm-10">
  <div class=""> 
    <div class="row"><div class="form-group col-md-2">
      <label for="email">Email:</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="email" style="width:130px">
    </div>
    <div class="form-group col-md-2">
      <label for="pwd">Password:</label>
      <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd" style="width:130px">
    </div>     
    <button type="submit" class="btn-primary">Submit</button>
     <button type="submit" class="btn-primary">Reset</button>
    </div>
</div>
</div>
</div>

5 个答案:

答案 0 :(得分:0)

您可以通过在行div中添加class =“ form-inline”来解决此问题,例如:

<div class="col-sm-10">
<div class="form-inline"> 
<div class="row"><div class="form-group col-md-2">
  <label for="email">Email:</label>
  <input type="email" class="form-control" id="email" placeholder="Enter email" name="email" style="width:130px">
</div>
<div class="form-group col-md-2">
  <label for="pwd">Password:</label>
  <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd" style="width:130px">
</div>     
<button type="submit" class="btn-primary">Submit</button>
 <button type="submit" class="btn-primary">Reset</button>
</div>

答案 1 :(得分:0)

Plunkr:http://next.plnkr.co/edit/ddVOoneUlEUSM2EO

我在这里做了几件事:

将btn类添加到您的按钮,以便它们具有与文本字段相同的高度。

我添加了一个空标签,该标签仅在中等或更高的断点处显示以填充垂直空间。

将按钮包装在自己的div中(因为标签是内联的)

将空标签和新div包装在自己的div.form-group.col

<div>
  <div class="col-sm-10">
    <div class=""> 
      <div class="row">
        <div class="form-group col-md-2">
          <label for="email">Email:</label>
          <input type="email" class="form-control" id="email" placeholder="Enter email" name="email" style="width:130px">
        </div>
        <div class="form-group col-md-2">
          <label for="pwd">Password:</label>
          <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd" style="width:130px">
        </div>  
        <div class="form-group col">   
          <label class="visible-md visible-lg">&nbsp;</label>
          <div>
            <button type="submit" class="btn btn-primary">Submit</button>
            <button type="submit" class="btn btn-primary">Reset</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

答案 2 :(得分:0)

在两个按钮中添加引导程序form-control类,并在两个按钮之间添加一些边距,以使其看起来更美观。

<button type="submit" class="btn-primary form-control">Submit</button>
<button type="submit" class="btn-primary form-control">Reset</button>

答案 3 :(得分:0)

这只是引导网格类问题。

确保使用col's类包装<div class="col-md-2">(即row)。

请参见下面给出的div结构。

<!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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">

  <div class="row">
      <div class="form-group col-md-2">
        <label for="email">Email:</label>
        <input type="email" class="form-control" id="email" placeholder="Enter email" name="email" style="width:130px">
      </div>
      <div class="form-group col-md-2">
        <label for="pwd">Password:</label>
        <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd" style="width:130px">
      </div>
      <div class="form-group col-md-2">
        <button type="submit" class="btn btn-primary">Submit</button>
        <button type="submit" class="btn btn-primary">Reset</button>
      </div>
  </div>
  
</div>
   

</body>
</html>

答案 4 :(得分:-2)

<div class="col-md-12">
   <div class="row">
      <div class="form-group col-md-2"><label for="email">Email:</label></div>
      <div class="form-group col-md-10"><input type="email" class="form-control" id="email" placeholder="Enter email" name="email" style="width:130px"></div>
   </div>
   <div class="row">
      <div class="form-group col-md-2"><label for="pwd">Password:</label></div>
      <div class="form-group col-md-10"><input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd" style="width:130px"></div>
   </div>
   <div class="row">
      <div class="form-group col-md-2"></div>
      <div class="form-group col-md-10"><button type="submit" class="btn-primary">Submit</button> &nbsp;<button type="submit" class="btn-primary">Reset</button></div>
   </div>
</div>