html - 如何使用 Bootstrap 垂直对齐表单?

标签 html css twitter-bootstrap-3 vertical-alignment

我正在尝试使用 Bootstrap 制作登录页面。到目前为止一切顺利,问题是垂直对齐。我想让登录表单贴在页面中央,所以我尝试阅读所有其他已回答的问题并尝试了很多不同的解决方案,但无济于事。我的代码如下。

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

最佳答案

有点hack,但我会想办法把它准确地放在中心。

只是尝试包裹盒子(.centerit)并移动它的位置。

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

关于html - 如何使用 Bootstrap 垂直对齐表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48349014/

相关文章:

css - 如何使 Bootstrap 3 导航栏中的按钮居中?

javascript - 如何加速我的 Javascript 游戏引擎

javascript - addEventListener 无故激活?

javascript - 如何使用jquery选择表中没有ID和类的嵌套元素

javascript - 从单独的文本区域标记和执行 HTML、CSS 和 JavaScript

javascript - 显示函数上的 Bootstrap 模式不传递值

jquery - 使用 Bootstrap 和表单验证 - 在输入字段的右侧获取刻度图像

css - DataTables 按钮不在窄屏幕上居中

asp.net - 如何确保网站不是从缓存中加载的

javascript - 使用 chartist.js 控制堆叠条周围的间距