UI Bootstrap / Bootstrap 3-如何居中放置容器?

时间:2020-03-07 23:07:59

标签: css angularjs twitter-bootstrap-3 angular-ui-bootstrap

我正在使用Angularjs创建一个简单的登录页面,并尝试使用UI Bootstrap向其添加CSS。 UI Bootstrap所需的依赖项之一是Bootstrap CSS,因此我的页面中也包含了它。现在,我的表单看起来符合预期;我要做的是将容器本身垂直居中,使其位于页面的中间。对我来说,有什么好方法吗?我已经搜索了很多解决方案,它们要么不做任何更改,要么以我不想要的方式压扁/更改表单本身。老实说,我不确定我居中容器的原因是否与UI Bootstrap或其他有关。

以下是表单外观的图片:Screenshot of the Form

这是我的代码:

<!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>

任何帮助将不胜感激,非常感谢。

1 个答案:

答案 0 :(得分:1)

尝试一下

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>