Search code examples
formstwitter-bootstrap-3alignment

bootstrap Dual column form


I'm new to bootstrap and trying to create a responsive form. On a large screen it should not take up the full screen but the fields should be split into two columns and centered, but on a small device the columns should take up the full width. In my code below I get the form to look the way I want on a small device but on a larger one I want it to be with two columns for the input fields and center the columns. I would appreciate some help in setting up the structure so that I can add the remaining fields I need and finish the design.

<!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.1.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>Horizontal form</h2>


  <form class="form-horizontal">
  <div class="row row-centered">
  <div class="col-xs-12 col-sm-6 col-centered">
    <div class="form-group">
      <label class="control-label col-sm-3" for="email">Email:</label>
      <div class="col-sm-4">
        <input type="email" class="form-control" id="email" placeholder="Enter email">
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-3" for="pwd">Password:</label>
      <div class="col-sm-4">          
        <input type="password" class="form-control" id="pwd" placeholder="Enter password">
      </div>
    </div>
    </div>

    <div class="col-xs-12 col-sm-6 col-centered">
    <div class="form-group">
      <label class="control-label col-sm-3" for="email">Email:</label>
      <div class="col-sm-4">
        <input type="email" class="form-control" id="email" placeholder="Enter email">
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-3" for="pwd">Password:</label>
      <div class="col-sm-4">          
        <input type="password" class="form-control" id="pwd" placeholder="Enter password">
      </div>
    </div>
    <div class="form-group">        
      <div class="col-sm-offset-2 col-sm-10">
        <div class="checkbox">
          <label><input type="checkbox"> 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">Submit</button>
      </div>
    </div>
    </div>

    </div>
  </form>

</div>

</body>
</html>

Solution

  • <div class="container">
      <h2>Horizontal form</h2>
    
    
          <form class="form-horizontal">
          <div class="row row-centered">
          <div class="col-xs-12 col-sm-6 col-centered">
            <div class="form-group">
              <label class="control-label col-sm-3" for="email">Email:</label>
              <div class="col-sm-9">
                <input type="email" class="form-control" id="email" placeholder="Enter email">
              </div>
            </div>
            <div class="form-group">
              <label class="control-label col-sm-3" for="pwd">Password:</label>
              <div class="col-sm-9">          
                <input type="password" class="form-control" id="pwd" placeholder="Enter password">
              </div>
            </div>
            </div>
    
            <div class="col-xs-12 col-sm-6 col-centered">
            <div class="form-group">
              <label class="control-label col-sm-3" for="email">Email:</label>
              <div class="col-sm-9">
                <input type="email" class="form-control" id="email" placeholder="Enter email">
              </div>
            </div>
            <div class="form-group">
              <label class="control-label col-sm-3" for="pwd">Password:</label>
              <div class="col-sm-9">          
                <input type="password" class="form-control" id="pwd" placeholder="Enter password">
              </div>
            </div>
            <div class="form-group">        
              <div class="col-sm-offset-2 col-sm-10">
                <div class="checkbox">
                  <label><input type="checkbox"> 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">Submit</button>
              </div>
            </div>
            </div>
    
            </div>
          </form>
    
        </div>
    
    
    
    
    
     Fiddler link is https://jsfiddle.net/j6jLhtc7/1/