Manav Sehgal
Manav Sehgal

Reputation: 27

How to put form contents in the same row in bootstrap

I've been trying to put the search bar and submit button within a form in the same row by assigning them different bootstrap column lengths but the form tag refuses to allow this.

<!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>

Upvotes: 0

Views: 57

Answers (2)

Robert
Robert

Reputation: 6967

In lieu of creating an entirely new class you could opt to use Bootstrap's built-in framework for handling forms. Depending on the specific version of Bootstrap you are using you would rely either on .input-group-addon or .input-group-append:

<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>

Note: You can replace 'Submit' with your Font Awesome icon, but you may need to apply additional styling to ensure that the button height remains properly lined up. I believe this is a bug in the most recent version of Bootstrap 4 (beta 3).

Upvotes: 0

Liam
Liam

Reputation: 6743

The simple way you can just create a div with new class with display:flex

.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>

Upvotes: 1

Related Questions