按钮水平对齐

时间:2018-08-14 16:21:29

标签: html css

查看代码

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

.background {
  height: 100%;
  background-image: url(../images/home-background.jpg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.button {
  border: none;
  padding: 15px 25px;
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  cursor: pointer;
  float: right;
  width: 28vw;
  border-radius: 50px;
  position: relative;
  margin-right: 7vw;
}

.button:hover {
  background-color: green;
}

.login {
  background-color: #DF6248;
  color: white;
  margin-top: 12vw;
}

.register {
  background-color: #DF6248;
  color: white;
  margin-top: 12vw;
}
<!DOCTYPE html>
<html>

<head>
  <title>HOME</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="../css/home.css">
</head>

<body class=background>
  <button class="button login">LOGIN</button>
  <button class="button register">REGISTER</button>
</body>

</html>

这是在水平对齐按钮。但我希望注册按钮位于登录按钮下方。 有什么我想念的吗??

我也尝试过 br ,但是效果不佳。 我该如何运作?

7 个答案:

答案 0 :(得分:0)

.layer { 
    margin: 10px 0 0 10px; 
    width:100%; 
    float: left;
}
.child { 
    float: both; 
    margin-left: 0px; 
    margin-top: 0px; 
}
<!DOCTYPE html>
    <html>
       <head>
          <title>HOME</title>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
       </head>
       <body>
          <div class="layer">
             <div class="child">1</div>
             <div class="child">2</div>
          </div>
       </body>
    </html>

答案 1 :(得分:0)

您的float:right类中有.button个。我将其删除并添加了display:block。研究float here

的行为

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

.background {
  height: 100%;
  background-image: url(../images/home-background.jpg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position:relative;
}

.button {
  border: none;
  padding: 15px 25px;
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  cursor: pointer;
  display: block;
  width: 28vw;
  border-radius: 50px;
  margin-right: 7vw;
}

.button:hover {
  background-color: green;
}

.login {
  background-color: #DF6248;
  color: white;
  margin-top: 5vw;
}

.register {
  background-color: #DF6248;
  color: white;
  margin-top: 5vw;
}
<!DOCTYPE html>
<html>

<head>
  <title>HOME</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="../css/home.css">
</head>

<body class=background>
  <button class="button login">LOGIN</button>
  <button class="button register">REGISTER</button>
</body>

</html>

答案 2 :(得分:0)

似乎您想将中间一个按钮对齐。 为使其保持响应并实现您想要的功能,我做了一些小改动:

更改了按钮css-

1)width30%

2)min-width150px

3)margin-right35% //calculation : (100% - 30%)/2

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

.background {
  height: 100%;
  background-image: url(../images/home-background.jpg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.button {
  border: none;
  padding: 15px 25px;
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  cursor: pointer;
  float: right;
  min-width: 150px;
  width: 30%;
  border-radius: 50px;
  position: relative;
  margin-right: 35%;
}

.button:hover {
  background-color: green;
}

.login {
  background-color: #DF6248;
  color: white;
  margin-top: 12vw;
}

.register {
  background-color: #DF6248;
  color: white;
  margin-top: 2vw;
}
<!DOCTYPE html>
<html>

<head>
  <title>HOME</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="../css/home.css">
</head>

<body class=background>
  <button class="button login">LOGIN</button>
  <button class="button register">REGISTER</button>
</body>

</html>

答案 3 :(得分:0)

只需将浮动按钮从按钮移动到容器div

<!doctype html>
  <html lang="en">
    <html>
      <style>
      body,
      html {
        margin: 0;
        height: 100%;
      }

      div{
      text-align:right;
      }

  .button {
    border: none;
    padding: 15px 25px;
    text-align: center;
    font-weight: bold;
    font-size: 16px;
    cursor: pointer;
    width: 28vw;
    border-radius: 50px;
    position: relative;
    margin-right: 7vw;
  }

  .button:hover {
    background-color: green;
  }

  </style>

  <head>
    <title>HOME</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="../css/home.css">
  </head>

  <body class=background>
  <div>
    <button class="button login">LOGIN</button>
  </div>
  <div>
    <button class="button register">REGISTER</button>
  </div>
  </body>

  </html>

答案 4 :(得分:0)

对按钮使用display: block会占用父块的整个宽度,因此以下按钮将放置在新行中。可以将auto用于水平边距来使按钮居中。

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

.background {
  height: 100%;
  background-image: url(../images/home-background.jpg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.button {
  border: none;
  display: block;
  padding: 15px 25px;
  margin: 2vw auto;
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  cursor: pointer;
  width: 28vw;
  border-radius: 50px;
  position: relative;
}

.login {
  background-color: #DF6248;
  color: white;
}

.register {
  background-color: #DF6248;
  color: white;
}

.button:hover {
  background-color: green;
}
<!DOCTYPE html>
<html>

<head>
  <title>HOME</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="../css/home.css">
</head>

<body class=background>
  <button class="button login">LOGIN</button>
  <button class="button register">REGISTER</button>
</body>

</html>

答案 5 :(得分:0)

.layer { 
  margin: 0 auto;
  display: table;
  margin-top: 34vh;
  /* you need to play a littl bit with the vh maybe something around 30-40vh*/
}

#child1, #child2 { 
float: both; 
margin-left: 0px; 
margin-top: 0px;  
}


.background {
  height: 100%;
  background-image: url(../images/home-background.jpg) no-repeat;
  background-position: center;
  background-size: cover;
}

.button {
  border: none;
  padding: 15px 25px;
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  cursor: pointer;
  width: 28vw;
  border-radius: 50px;
  background-color: #DF6248;
  color: white;
}

.button:hover {
  background-color: green;
}
<!DOCTYPE html>
<html>
<head>
    <title>HOME</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="../css/home.css">
</head>
<body>
<body class="background">
  <div class="layer">
  <div class="child">
    <button class="button child">LOGIN</button>
  </div>
  <br>
  <br>
  <div class="child">
    <button class="button child">REGISTER</button>
  </div>
</div>
</body>
</body>
</html>

答案 6 :(得分:0)

您可以将每个按钮包装在div中

•TFS_PRODUCT_VERSION = 14.95.25122.0
•TFS_SERVICE_LEVEL = Dev14.M95.3
body,
html {
  margin: 0;
  height: 100%;
}

.background {
  height: 100%;
  background-image: url(../images/home-background.jpg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.btn-container {
  text-align: center;
}

.button {
  border: none;
  padding: 15px 25px;
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  cursor: pointer;
  float: none;
  clear: both;
  width: 28vw;
  border-radius: 50px;
  position: relative;
  margin-right: 7vw;
}

.button:hover {
  background-color: green;
}

.login {
  background-color: #DF6248;
  color: white;
  margin-top: 12vw;
}

.register {
  background-color: #DF6248;
  color: white;
  margin-top: 12vw;
}