如何将表单内容放在bootstrap的同一行中

时间:2018-01-03 19:58:35

标签: html css twitter-bootstrap bootstrap-4

我一直在尝试将搜索栏和提交按钮放在同一行的表单中,方法是为它们分配不同的引导列长度,但表单标签拒绝允许这样做。



<!doctype html>
<html lang="en">

  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script defer src="https://use.fontawesome.com/releases/v5.0.2/js/all.js"></script>

    <body>
      <br>
      <div class="input-group col-lg-12">
        <form method="post">
          <input type="text" class="form-control input-lg" placeholder="Ex: WRT 102" />
          <button type="submit"><i class="fa fa-search"></i></button>
        </form>
      </div>
    </body>
  </head>

</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

使用display:flex

创建包含新类的div的简单方法

.flex{
  display:flex;
}
<html lang="en">

  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script defer src="https://use.fontawesome.com/releases/v5.0.2/js/all.js"></script>

    <body>
      <br>
      <div class="input-group col-lg-12">
        <form method="post">
        <div class="flex">
          <input type="text" class="form-control input-lg" placeholder="Ex: WRT 102" />
          <button type="submit"><i class="fa fa-search"></i></button>
          </div>
        </form>
      </div>
    </body>
  </head>

</html>

答案 1 :(得分:0)

代替创建一个全新的类,您可以选择使用Bootstrap的内置框架来处理表单。根据您使用的Bootstrap的特定版本,您可以依赖.input-group-addon.input-group-append

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>


<form method="post">

<div class="input-group">
	<input type="text" class="form-control" placeholder="Ex: WRT 102" />
	<div class="input-group-append">
       <button type="submit" class="btn btn-secondary">Submit</button>
	</div>
</div>

</form>
&#13;
&#13;
&#13;

注意:您可以替换&#39;提交&#39;使用Font Awesome图标,但您可能需要应用其他样式以确保按钮高度保持正确排列。我相信这是最新版本的Bootstrap 4(beta 3)中的一个错误。