Search code examples
htmlcsstwitter-bootstrap-3vertical-alignment

How do I vertically align a form using bootstrap?


I'm trying to make a login page using Bootstrap. So far so good, the problem is the vertical align. I want to make the login form stick to the center of the page, so I tryed reading all the other answered questions and tryed really a lot of different solutions, but to no avail. My code is the following.

.container {
  display: table;
  table-layout: fixed;
}

.form-horizontal {
  width: 500px;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">


<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">


<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<link href="site_index.css" type="text/css" rel="stylesheet">

</head>

<body>

  <div class="container">

    <form class="form-horizontal">
      <div class="form-group">
        <label for="username" class="col-sm-2 control-label">Username</label>
        <div class="col-sm-10">
          <input type="email" class="form-control" id="username" placeholder="username" size="16">
        </div>
      </div>
      <div class="form-group">
        <label for="password" class="col-sm-2 control-label">Password</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" id="password" placeholder="password" size="16">
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-primary btn-lg btn-block" id="signin_button" value="Sign In">Sign in</button>
        </div>
      </div>
    </form>

  </div>



  <footer id="w3c">
    <hr>
    <p>
      Results and page (C) Copyright NerdLuv Inc.
    </p>
    <a href="http://validator.w3.org/check/referer">
      <img src="http://www.cs.washington.edu/education/courses/cse190m/12sp/homework/4/w3c-html.png" alt="Valid HTML">
    </a>
    <a href="http://jigsaw.w3.org/css-validator/check/referer">
      <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS">
    </a>
  </footer>


Solution

  • It's kind of hack, but I will try to figure how I can place it exactly in the center.

    Just tried to wrap the box(.centerit) and move it's location.

    .container {
      display: table;
      table-layout: fixed;
    }
    
    .form-horizontal {
      width: 500px;
      display: table-cell;
      vertical-align: middle;
      text-align: center;
    }
    
    #outer{
      height:800px;
      border:2px solid orange;
    }
    
    .centerit{
      border:3px solid purple;
      top:30%;
      position:relative;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <div id="outer">
    <div class="centerit">
    <div class="container">
    
      <form class="form-horizontal">
        <div class="form-group">
          <label for="username" class="col-sm-2 control-label">Username</label>
          <div class="col-sm-10">
            <input type="email" class="form-control" id="username" placeholder="username" size="16">
          </div>
        </div>
        <div class="form-group">
          <label for="password" class="col-sm-2 control-label">Password</label>
          <div class="col-sm-10">
            <input type="password" class="form-control" id="password" placeholder="password" size="16">
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary btn-lg btn-block" id="signin_button" value="Sign In">Sign in</button>
          </div>
        </div>
      </form>
    
    </div>
    
    
    
    <footer id="w3c">
      <hr>
      <p>
        Results and page (C) Copyright NerdLuv Inc.
      </p>
      <a href="http://validator.w3.org/check/referer">
        <img src="http://www.cs.washington.edu/education/courses/cse190m/12sp/homework/4/w3c-html.png" alt="Valid HTML">
      </a>
      <a href="http://jigsaw.w3.org/css-validator/check/referer">
        <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS">
      </a>
    </footer>
    </div>
    </div>