All for Joomla All for Webmasters

Bootstrap Forms

In Bootstrap, there are three types of form layouts:

  • Vertical form (this is default)
  • Horizontal form
  • Inline form

Bootstrap Form Rules

There are three standard rules for these 3 form layouts:

  • Always use <form role=”form”> (helps improve accessibility for people using screen readers)
  • Wrap labels and form controls in <div class=”form-group”> (needed for optimum spacing)
  • Add class .form-control to all textual <input>, <textarea>, and <select> elements

1) Bootstrap Vertical Form (Default)

  1.    <!DOCTYPE html>
  2. <html lang=“en”>
  3.   <head>
  4.      <title>Bootstrap example</title>
  5.      <link rel=“stylesheet” href=“https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css”/>
  6.   </head>
  7.   <body>
  8. <div class=“container”>
  9.   <h1>Vertical Form Example</h1>
  10. <form style=“width:300px”>
  11.   <div class=“form-group”>
  12.     <label for=“exampleInputEmail1”>Email address</label>
  13.     <input type=“email” class=“form-control” id=“exampleInputEmail1” placeholder=“Email”>
  14.   </div>
  15.   <div class=“form-group”>
  16.     <label for=“exampleInputPassword1”>Password</label>
  17.     <input type=“password” class=“form-control” id=“exampleInputPassword1” placeholder=“Password”>
  18.   </div>
  19.   <button type=“submit” class=“btn btn-default”>Login</button>
  20. </form>
  21. </div>
  22. <script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js”></script>
  23.   <script src=“http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js”></script>
  24.   </body>
  25. </html>

 


2) Bootstrap Inline Form

In Bootstrap Inline forms, all elements are inline, left-aligned, and the labels are alongside.

This example is only applied to forms within viewports that are at least 768px wide!

Example:

  1. <!DOCTYPE html>
  2. <html lang=“en”>
  3. <head>
  4.   <title>Bootstrap Example</title>
  5.   <meta charset=“utf-8”>
  6.   <meta name=“viewport” content=“width=device-width, initial-scale=1”>
  7.   <link rel=“stylesheet” href=“http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css”>
  8. </head>
  9. <body>
  10. <div class=“container”>
  11.   <h2>Inline form Example</h2>
  12.   <form class=“form-inline” role=“form”>
  13.     <form style=“width:300px”>
  14.   <div class=“form-group”>
  15.     <label for=“exampleInputEmail1”>Email address</label>
  16.     <input type=“email” class=“form-control” id=“exampleInputEmail1” placeholder=“Email”>
  17.   </div>
  18.   <div class=“form-group”>
  19.     <label for=“exampleInputPassword1”>Password</label>
  20.     <input type=“password” class=“form-control” id=“exampleInputPassword1” placeholder=“Password”>
  21.   </div>
  22.   <button type=“submit” class=“btn btn-default”>Login</button>
  23. </form>
  24. </div>
  25. <script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js”></script>
  26.   <script src=“http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js”></script>
  27. </body>
  28. </html>

 


3) Bootstrap Horizontal Form

You have to add some additional rules if you want to create a horizontal form.

Additional rules for a horizontal form:

  • Add class .form-horizontal to the <form> element
  • Add class .control-label to all <label> elements

Example:

  1. <!DOCTYPE html>
  2. <html lang=“en”>
  3. <head>
  4.   <title>Bootstrap Example</title>
  5.   <meta charset=“utf-8”>
  6.   <meta name=“viewport” content=“width=device-width, initial-scale=1”>
  7.   <link rel=“stylesheet” href=“http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css”>
  8. </head>
  9. <body>
  10. <div class=“container”>
  11.   <h2>Horizontal form Example</h2>
  12.   <form class=“form-horizontal” role=“form”>
  13.     <form style=“width:300px”>
  14.   <div class=“form-group”>
  15.       <label class=“control-label col-sm-2” for=“email”>Email:</label>
  16.       <div class=“col-sm-10”>
  17.         <input type=“email” class=“form-control” id=“email” placeholder=“Enter email”>
  18.       </div>
  19.     </div>
  20.     <div class=“form-group”>
  21.       <label class=“control-label col-sm-2” for=“pwd”>Password:</label>
  22.       <div class=“col-sm-10”>
  23.         <input type=“password” class=“form-control” id=“pwd” placeholder=“Enter password”>
  24.       </div>
  25.     </div>
  26.   <div class=“form-group”>
  27.       <div class=“col-sm-offset-2 col-sm-10”>
  28.         <button type=“submit” class=“btn btn-default”>Submit</button>
  29.       </div>
  30.     </div>
  31.   </form>
  32. </div>
  33. <script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js”></script>
  34. <script src=“http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js”></script>
  35. </body>
  36. </html>
PinIt
submit to reddit

Leave a Reply

Top