我怎样才能以这个为中心?

时间:2021-06-24 22:19:37

标签: html css flexbox

我想将 h1“登录”居中。我试过使用 margin: left 但当我使用它时,按钮也会由于某种原因移动。我已将 justify-content: center; 用于容器,但自从我添加按钮后,“登录”h1 已移动,我不知道该怎么做才能再次将其完美地放在中间。

https://codepen.io/i-am-programming/pen/gOmydqv

* {
  margin: 0;
  padding: 0;
  font-size: 25px;
  font-family: "Montserrat", sans-serif;
}

body {
  background: url("images/background.jpeg");
  background-size: cover;
}

a {
  text-decoration: none;
  margin-right: 30px;
  color: #fff;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 8px;
  transition: 0.3s ease;
}

nav {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
}

a:hover {
  color: black;
  background-color: rgba(255, 255, 255, 0.7);
}

.container {
  display: flex;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  height: 450px;
  background-color: rgba(0, 0, 0, 0.7);
  justify-content: center;
}

.login {
  color: white;
  margin-top: 50px;
  font-size: 45px;
}

input {
  height: 30px;
  width: 300px;
  outline: none;
}

.username {
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.pass {
  position: absolute;
  top: 65%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.button {
  width: 150px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, 1200%);
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      href="https://fonts.googleapis.com/css2?family=Montserrat:wght@200&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="styles.css" />
    <title>Document</title>
  </head>
  <body>
    <nav>
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">Store</a>
      <a href="#">Contact Us </a>
    </nav>
    <div class="container">
      <h1 class="login">Log In</h1>
      <input type="text" placeholder="Username" class="username" />
      <input type="password" placeholder="Password" class="pass" />
      <input type="button" class="button" value="Submit" />
    </div>

    <script src="app.js"></script>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

position: absolute; 添加到 .login 您可能还需要将 .button 的转换更改为: transform: translate(0%, 1200%);

* {
  margin: 0;
  padding: 0;
  font-size: 25px;
  font-family: "Montserrat", sans-serif;
}

body {
  background: url("images/background.jpeg");
  background-size: cover;
}

a {
  text-decoration: none;
  margin-right: 30px;
  color: #fff;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 8px;
  transition: 0.3s ease;
}

nav {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
}

a:hover {
  color: black;
  background-color: rgba(255, 255, 255, 0.7);
}

.container {
  display: flex;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  height: 450px;
  background-color: rgba(0, 0, 0, 0.7);
  justify-content: center;
}

.login {
  position: absolute;
  color: white;
  margin-top: 50px;
  font-size: 45px;
}

input {
  height: 30px;
  width: 300px;
  outline: none;
}

.username {
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.pass {
  position: absolute;
  top: 65%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.button {
  width: 150px;
  top: 50%;
  left: 50%;
  transform: translate(0%, 1200%);
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      href="https://fonts.googleapis.com/css2?family=Montserrat:wght@200&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="styles.css" />
    <title>Document</title>
  </head>
  <body>
    <nav>
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">Store</a>
      <a href="#">Contact Us </a>
    </nav>
    <div class="container">
      <h1 class="login">Log In</h1>
      <input type="text" placeholder="Username" class="username" />
      <input type="password" placeholder="Password" class="pass" />
      <input type="button" class="button" value="Submit" />
    </div>

    <script src="app.js"></script>
  </body>
</html>

答案 1 :(得分:1)

h1 {
  position: absolute;
  top: 0;
  left: 0;
  text-align: center;
  width: 100%;
}


/* or add to .login*/

.login {
  /*...*/
  position: absolute;
  text-align: center;
  width: 100%;
}