代码之家  ›  专栏  ›  技术社区  ›  amit_vickey

按钮水平对齐

  •  0
  • amit_vickey  · 技术社区  · 6 年前

    检查代码

    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>

    这是做什么水平对齐按钮。但是我想要 按钮位于 登录

    我试过了 比尔 也一样,但效果不太好。 我该怎么做?

    7 回复  |  直到 6 年前
        1
  •  0
  •   Aniket Kadam    6 年前

    只需将浮点数从按钮移到容器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>
    
        2
  •  0
  •   Salvatore    6 年前

    你好像想把中间的按钮一个接一个地排列起来。 为了保住它 反应敏捷的 为了实现你想要的,我做了一些小的改变:

    更改了按钮css-

    1) width 30%

    2) min-width 150px

    margin-right 35% //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
  •   user8413951 user8413951    6 年前

    .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>
        4
  •  0
  •   Ramesh Diego Lins de Freitas    6 年前

    你有 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>
        5
  •  0
  •   Alexander    6 年前

    使用 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>
        6
  •  0
  •   G. Tranter    6 年前

    .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>
        7
  •  0
  •   Razvan Zamfir    6 年前

    你可以把每个按钮包在一个div里

    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;
    }
    <!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>
      <div class="btn-container">
        <button class="button login">LOGIN</button>
      </div>
      <div class="btn-container">
        <button class="button register">REGISTER</button>
      </div>
    </body>
    
    </html>