定位表单和多个图像

时间:2017-10-04 08:32:15

标签: html css vertical-alignment centering

我在垂直和水平方向上居中问题时,我设法将表单水平居中,但是无法设置为垂直居中而不会导致表单和图像重叠。还有问题在页面底部水平居中多个图像。



.banner {
  position: relative;
  width: 100%;
  top: 0;
  left: 0;
  padding: 0;
  margin: 0;
  background-color: #595959;
  color: #fff;
  text-align: center;
  font-family: 'Lato', sans-serif;
  font-size: 25px;
  padding-top: 50px;
}

#imagesMain {
  position: relative;
  max-width: 75rem;
  left: 0;
  right: 0;
  margin: auto;
  bottom: 0;
  padding: 0;
  margin: 20px auto;
  text-align: center;
}

#imagesMain a {
  width: 30%;
  float: left;
  position: relative;
  margin: 1.5%;
}

img {
  max-width: 100%;
  height: auto;
  margin-right: 25px;
  position: relative;
}

body,
html {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  vertical-align: middle;
}

#form {
  display: table;
  margin: 0 auto;
}

<!DOCTYPE html>

<body>
  <div class="banner">
    <h1>A-Level Revision Website</h1>
  </div>
  <div id="form">
    <form id="loginForm">
      <input type="email" class="login-username" autofocus="true" required="true" placeholder="Email" /><br>
      <input type="password" class="login-password" required="true" placeholder="Password" /><br>
      <input type="submit" name="Login" value="Login" class="login-submit" /><br>
      <a href="#" class="login-forgot-pass">forgot password?</a>
    </form>
  </div>
  <div id="imagesMain">
    <a href="Maths.html">
			<img src="https://i.pinimg.com/originals/06/9a/c8/069ac81cbe0e3a6f8fb43a0df42125a0.jpg" alt="Maths"">
		</a>
    <a href="ComputerScience.html">
      <img src="https://i.pinimg.com/originals/06/9a/c8/069ac81cbe0e3a6f8fb43a0df42125a0.jpg" alt="ComputerScience">
    </a>
    <a href="Physics.html">  
			<img src="https://i.pinimg.com/originals/06/9a/c8/069ac81cbe0e3a6f8fb43a0df42125a0.jpg" alt="Physics"">
		</a>
  </div>

  </html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以使用 Flexbox 执行此类操作:

&#13;
&#13;
* {margin: 0; padding: 0; box-sizing: border-box}
body, html {height: 100%}

#container {
  display: flex; /* displays flex-items (children) inline */
  flex-direction: column; /* stacks them vertically */
  height: 100%;
}

.banner,
#loginForm,
.images {
  flex: 1;
}

/* Another example:
.banner {flex: 1}
#loginForm {flex: 2} Means that it takes twice as much space as .banner and .images (100% / 4 (in flex values) = 25%, i.e. flex: 1 = 25%).
.images {flex: 1}
*/

.banner {
  display: flex;
  justify-content: center; /* centers them horizontally */
  align-items: center; /* centers them vertically */
  text-align: center;
  background: #595959;
  color: #fff;
  font-family: 'Lato', sans-serif;
  font-size: 25px;
}

#loginForm {
  display: flex; /* also given flex properties */
  flex-direction: column; /* stacks the login form elements vertically */
  justify-content: center; /* aligns the form horizontally */
  align-items: center; /* aligns the form vertically */
  margin: 10px 0;
}

#loginForm > input {
  margin-bottom: 5px;
  padding: 5px;
}

.images {
  display: flex; /* also given flex properties */
  justify-content: center; /* aligns images horizontally */
  align-items: flex-end; /* aligns images at the bottom */
  padding: 10px 5px; /* for better presentation */
  background: #595959; /* for better presentation */
}

.images > a {
  margin: 0 5px;
}

.images > a > img {
  display: block;
  max-width: 100%;
}
&#13;
<div id="container">
  <div class="banner">
    <h1>A-Level Revision Website</h1>
  </div>
  <form id="loginForm">
    <input type="email" class="login-username" placeholder="E-mail" autofocus="true" required>
    <input type="password" class="login-password" placeholder="Password" required>
    <input type="submit" class="login-submit" name="Login" value="Login">
    <a href="#" class="login-forgot-pass">Forgot password?</a>
  </form>
  <div class="images">
    <a href="Maths.html"><img src="https://i.pinimg.com/originals/06/9a/c8/069ac81cbe0e3a6f8fb43a0df42125a0.jpg" alt="Maths"></a>
    <a href="ComputerScience.html"><img src="https://i.pinimg.com/originals/06/9a/c8/069ac81cbe0e3a6f8fb43a0df42125a0.jpg" alt="ComputerScience"></a>
    <a href="Physics.html"><img src="https://i.pinimg.com/originals/06/9a/c8/069ac81cbe0e3a6f8fb43a0df42125a0.jpg" alt="Physics"></a>
  </div>
</div>
&#13;
&#13;
&#13;

此解决方案完全响应。我为flex的所有三个孩子 1 提供了height(100%/ 3({ {1}}值)= 33.33%,即flex = 33.33%)。 如果您愿意,您可以给每个孩子一个不同的价值,例如flex: 1 #container > .banner {flex: 1;} #container > #loginForm {flex: 2;}或任何其他数字(也可以使用小数),看看他们是如何回应的。

您可以根据需要调整#container > .images {flex: 3;} 以获得所需的结果,但请注意flex.banner的不同值会导致{{ 1}}屏幕上没有垂直居中。另请注意,如果 flex-item 中内容(.images s)的原始#loginForm在某个时刻大于height item 本身,然后会影响 flex-items img