如何使用引导程序垂直对齐表单?

时间:2018-01-19 20:26:24

标签: 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>

1 个答案:

答案 0 :(得分:1)

这是一种黑客行为,但我会试着想办法如何把它放在中心位置。

试图包装框(.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; })并移动它的位置。

&#13;
&#13;
<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>
&#13;
$TheArray | %{$_.TheCollection | %{$_.TheProperty}}
&#13;
&#13;
&#13;