UI Bootstrap/Bootstrap 3 – How do I center a container?

Issue

I’m creating a simple login page with Angularjs, and am trying to add CSS to it using UI Bootstrap. One of the dependencies that UI Bootstrap required was Bootstrap CSS, so I have that included in my page too. Right now, my form looks as intended; what I want to do is to center the container itself vertically, so that it sits in the middle of the page. Is there a good way for me to go about doing that? I’ve searched up a lot of solutions, and they either do not change anything, or squash/change the form itself in a way that I don’t want. And to be honest, I’m not sure if the reason I’m having difficulty centering the container has to do with UI Bootstrap or anything.

Here is an image of what the form looks like: Screenshot of the Form

And here is my code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script>
  </head>
  <body>
    <!-- This is the div for the form component -->
    <div ng-app="form-component">
      <div class="container">
        <div class="row center-block">
          <div class="col-sm-4"></div>
          <div class="col-sm-4">
            <form>
              <div class="form-group">
                <label for="username">Username</label>
                <input type="text" class="form-control" id="username-input">
              </div>
              <div class="form-group">
                <label for="password">Password</label>
                <input type="password" class="form-control" id="password-input">
              </div>
              <div class="text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
              </div>
            </form>
          </div> <!-- class="col-sm-4" -->
          <div class="col-sm-4"></div>
        </div> <!-- class="row justify-content-center" -->
      </div> <!-- class="container" -->
    </div> <!-- np-app form-component -->
    <script>
      var form = angular.module("form-component", ["ui.bootstrap"]);
    </script>
  </body>
</html>

Any help would be greatly appreciated, thank you very much.

Solution

Try this

html, body {
  height: 100%
}

.wrapper {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script>
  </head>
  <body>
    <!-- This is the div for the form component -->
    <div ng-app="form-component" class="wrapper">
      <div class="container">
        <div class="row center-block">
          <div class="col-sm-4"></div>
          <div class="col-sm-4">
            <form>
              <div class="form-group">
                <label for="username">Username</label>
                <input type="text" class="form-control" id="username-input">
              </div>
              <div class="form-group">
                <label for="password">Password</label>
                <input type="password" class="form-control" id="password-input">
              </div>
              <div class="text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
              </div>
            </form>
          </div> <!-- class="col-sm-4" -->
          <div class="col-sm-4"></div>
        </div> <!-- class="row justify-content-center" -->
      </div> <!-- class="container" -->
    </div> <!-- np-app form-component -->
    <script>
      var form = angular.module("form-component", ["ui.bootstrap"]);
    </script>
  </body>
</html>

Answered By – Mohammad

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published