我的div元素并不完全居中。 (他们看起来像是但不是。)

时间:2017-03-22 17:13:59

标签: html css

我正在创建自己的登录页面。现在只是一种HTML风格。

正如您可能看到的那样,通过下面的jsfiddle,我制作的文件略微偏离中心。我想完全纵向和横向对齐所有内容。

这是JsFiddle,https://jsfiddle.net/BeastFox/jgLsdmkk/

这是源代码,

<!DOCTYPE html>
<html>

  <head>
    <title>Login</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="main.css">
    <link href="https://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet">
  </head>

  <body>

<div class="LoginBox" style="">
<div class="element_wrapper">
 <a class="Login">Login</a>


    <input type="text" name="" class="username" placeholder="Username">
    <input type="password" name="" class="password" placeholder="Password">


    <class>
    <center><button class="btn" onclick="myFunction()">Login</button>
        <a href="https://www.beastfox.com"><input type="submit" value="Create" class="btn2"/></a>




</center>
        </class>
</div>
 </div>

  </body>

</html>

这是CSS:

body,
html 
{
  background:
    linear-gradient(27deg, #151515 5px, transparent 5px) 0 5px,
    linear-gradient(207deg, #151515 5px, transparent 5px) 10px 0px,
    linear-gradient(27deg, #222 5px, transparent 5px) 0px 10px,
    linear-gradient(207deg, #222 5px, transparent 5px) 10px 5px,
    linear-gradient(90deg, #1b1b1b 10px, transparent 10px),
    linear-gradient(#1d1d1d 25%, #1a1a1a 25%, #1a1a1a 50%, transparent 50%, transparent 75%, #242424 75%, #242424);
  background-color: #131313;
  background-size: 20px 20px;

  overflow-x: hidden;
  overflow-y: hidden;
}

.username {
  display: block;
  margin: 0 auto;
  background: #333;
  color: #ccc;
  width: 150px;
  padding: 6px 15px 6px 5px;
  transition: 500ms all ease;
  border: 1px solid #333;
  margin-bottom: 10px;
  text-align: center;
  vertical-align: middle;
}

.username:hover {
  box-shadow: 0px 0px 4px #000;
}

.password {
  padding-top: 3;
  text-align: center;
  display: block;
  margin: 0 auto;
  background: #333;
  color: #ccc;
  width: 150px;
  padding: 6px 15px 6px 5px;
  transition: 500ms all ease;
  border: 1px solid #333;
}

.password:hover {
  box-shadow: 0px 0px 4px #000;
}

.LoginBox {
  top: 40%;
  left: 50%;
  margin: -100px 0 0 -150px;
  position: fixed;
  padding-top: 50 auto;
  width: 300px;
  height: 300px;
  padding: 25px;
  background-color: firebrick;
  z-index: -3;
  box-shadow: 0px 0px 9px #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.Login {
  display: block;
  width: 100%;
  text-align: center;
  font-size: 30px;
  font-family: 'Roboto Condensed', sans-serif;
  color: #FFF;
  margin-bottom: 1em;
  border-bottom: 2px solid #ccc;
}

.btn {
    margin-top: 3em;
    top 70%;
    width: 70px;
    height: 30px;
    background-color: #333;
    border: none;
    color: #ccc;
    cursor: pointer;
    margin-right: 2em;
}
.btn:hover {
    box-shadow: 0px 0px 4px #000;
}
.btn:active {
    box-shadow: 0px 0px 5px #000;
}
.btn:focus {outline:none;}
.btn2 {
    margin-top: 3em;
    top 70%;
    width: 70px;
    height: 30px;
    background-color: #ccc;
    border: none;
    color: #333;
    cursor: pointer;
}
.btn2:hover {
    box-shadow: 0px 0px 4px #000;
}
.btn2:active {
    box-shadow: 0px 0px 5px #000;
}

3 个答案:

答案 0 :(得分:2)

使用以下设置代替那些负边距:

position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

https://jsfiddle.net/ccLg8agu/1/(已更新,之前未包含所有正确的设置)

答案 1 :(得分:1)

而不是top: 40%和负的固定边距,您希望使用top: 50%; left: 50%; transform: translate(-50%,-50%);的组合来绝对居中.LoginBox

您可以在此处阅读有关居中技巧的更多信息https://www.w3.org/Style/Examples/007/center.en.html

&#13;
&#13;
body,
html {
  background: linear-gradient(27deg, #151515 5px, transparent 5px) 0 5px, linear-gradient(207deg, #151515 5px, transparent 5px) 10px 0px, linear-gradient(27deg, #222 5px, transparent 5px) 0px 10px, linear-gradient(207deg, #222 5px, transparent 5px) 10px 5px, linear-gradient(90deg, #1b1b1b 10px, transparent 10px), linear-gradient(#1d1d1d 25%, #1a1a1a 25%, #1a1a1a 50%, transparent 50%, transparent 75%, #242424 75%, #242424);
  background-color: #131313;
  background-size: 20px 20px;
  overflow-x: hidden;
  overflow-y: hidden;
}

.username {
  display: block;
  margin: 0 auto;
  background: #333;
  color: #ccc;
  width: 150px;
  padding: 6px 15px 6px 5px;
  transition: 500ms all ease;
  border: 1px solid #333;
  margin-bottom: 10px;
  text-align: center;
  vertical-align: middle;
}

.username:hover {
  box-shadow: 0px 0px 4px #000;
}

.password {
  padding-top: 3;
  text-align: center;
  display: block;
  margin: 0 auto;
  background: #333;
  color: #ccc;
  width: 150px;
  padding: 6px 15px 6px 5px;
  transition: 500ms all ease;
  border: 1px solid #333;
}

.password:hover {
  box-shadow: 0px 0px 4px #000;
}

.LoginBox {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  position: fixed;
  padding-top: 50 auto;
  width: 300px;
  height: 300px;
  padding: 25px;
  background-color: firebrick;
  z-index: -3;
  box-shadow: 0px 0px 9px #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.Login {
  display: block;
  width: 100%;
  text-align: center;
  font-size: 30px;
  font-family: 'Roboto Condensed', sans-serif;
  color: #FFF;
  margin-bottom: 1em;
  border-bottom: 2px solid #ccc;
}

.btn {
  margin-top: 3em;
  top 70%;
  width: 70px;
  height: 30px;
  background-color: #333;
  border: none;
  color: #ccc;
  cursor: pointer;
  margin-right: 2em;
}

.btn:hover {
  box-shadow: 0px 0px 4px #000;
}

.btn:active {
  box-shadow: 0px 0px 5px #000;
}

.btn:focus {
  outline: none;
}

.btn2 {
  margin-top: 3em;
  top 70%;
  width: 70px;
  height: 30px;
  background-color: #ccc;
  border: none;
  color: #333;
  cursor: pointer;
}

.btn2:hover {
  box-shadow: 0px 0px 4px #000;
}

.btn2:active {
  box-shadow: 0px 0px 5px #000;
}
&#13;
<!DOCTYPE html>
<html>

  <head>
    <title>Login</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="main.css">
    <link href="https://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet">
  </head>

  <body>

    <div class="LoginBox" style="">
      <div class="element_wrapper">
        <a class="Login">Login</a>


        <input type="text" name="" class="username" placeholder="Username">
        <input type="password" name="" class="password" placeholder="Password">


        <class>
          <center>
            <button class="btn" onclick="myFunction()">Login</button>
            <a href="https://www.beastfox.com">
              <input type="submit" value="Create" class="btn2" />
            </a>




          </center>
        </class>
      </div>
    </div>

  </body>

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

答案 2 :(得分:0)

问题是您还要向.LoginBox添加填充,这会添加到整体宽度。因此-150px页边距不正确。

一种方法是使用*{box-sizing:border-box},这会使borderpadding成为width中的容器(他们宽度而不是添加到它。)

https://jsfiddle.net/jgLsdmkk/1/

更新了演示

另一个是相应地调整你的值(通过在计算元素的大小时考虑填充)