Bootstrap Forms

Form act as a container for form elements. All textual input, textarea, and select elements with class .form-control have a width of 100%.

Bootstrap provides following three types of form layouts:
• Vertical form (this is default)
• Horizontal form
• Inline form

Standard rules for Vertical form layouts:
• Wrap labels and form controls in div class=”form-group.
• Add class .form-control to all textual input, textarea, and select elements.

Bootstrap Vertical Form Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Vertical Form 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">
  <h2>Bootstrap Vertical Form Example</h2>
  <form action="/test.php">
    <div class="form-group">
      <label for="username">UserName:</label>
      <input type="email" class="form-control" id="username" placeholder="Enter UserName" name="username">
    </div>
    <div class="form-group">
      <label for="password">Password:</label>
      <input type="password" class="form-control" id="password" placeholder="Enter Password" name="password">
    </div>
    <div class="checkbox">
      <label><input type="checkbox" name="remember"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default">Login</button>
  </form>
</div>
 
</body>
</html>

Output:

Bootstrap Inline Form

In bootstrap inline form, all of the elements are inline, left-aligned, and the labels are alongside.

Note: This example applies to forms within view ports that are at least 768px wide!

Standard rules for Inline form layouts:
• Wrap labels and form controls in div class=”form-group.
• Add class .form-control to all textual input, textarea, and select elements.
• Add class .form-inline to the

element

Bootstrap Inline Form Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Inline Form 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">
  <h2>Bootstrap Inline Form Example</h2>
  <form class="form-inline" action="/test.php">
    <div class="form-group">
      <label for="username">UserName:</label>
      <input type="email" class="form-control" id="username" placeholder="Enter UserName" name="username">
    </div>
    <div class="form-group">
      <label for="password">Password:</label>
      <input type="password" class="form-control" id="password" placeholder="Enter Password" name="password">
    </div>
    <div class="checkbox">
      <label><input type="checkbox" name="remember"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default">Login</button>
  </form>
</div>
 
</body>
</html>

Output:

Bootstrap Horizontal Form

A horizontal form refers that the labels are aligned next to the input field (horizontal) on large and medium screens. On small screens (767px and below), it will transform to a vertical form (labels are placed on top of each input).

Standard rules for horizontal form layouts:
• Wrap labels and form controls in div class=”form-group.
• Add class .form-control to all textual input, textarea, and select elements.
• Add class .form-horizontal to the form element
• Add class .control-label to all label elements

Bootstrap Horizontal Form Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Horizontal Form 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">
  <h2>Bootstrap Horizontal Form Example</h2>
  <form class="form-horizontal" action="/test.php">
    <div class="form-group">
      <label class="control-label col-sm-2" for="email">UserName:</label>
      <div class="col-sm-10">
        <input type="email" class="form-control" id="username" placeholder="Enter username" name="username">
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-2" for="pwd">Password:</label>
      <div class="col-sm-10">          
        <input type="password" class="form-control" id="password" placeholder="Enter password" name="password">
      </div>
    </div>
    <div class="form-group">        
      <div class="col-sm-offset-2 col-sm-10">
        <div class="checkbox">
          <label><input type="checkbox" name="remember"> 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">Login</button>
      </div>
    </div>
  </form>
</div>
 
</body>
</html>

Output: