对齐复选框和按钮

时间:2020-04-18 20:45:15

标签: html css

我正在尝试使用用户名和密码在登录表单下对齐,左侧的复选框是“还记得我吗?”右边是一个Login按钮,但是我无法弄清楚如何正确对齐它们。当前它们不在所需位置并且看起来很糟糕 复选框和登录按钮的代码位于css的末尾-label.custom-checkbox和.loginbutton

    <head>
    <link rel="stylesheet" type="text/css" href="form.css">
    <style>

body, html, div, p {
    margin: 0px;
    padding: 0px;
}
body {
    padding: 5px;
    font-family: Verdana;
}

#loginbox {
    width: 700px;
    height: 300px;
    background: #e6e6e6;
}

#heding {
    font-family: Verdana;
    font-weight: 600;
    font-size: 22px;
    color: #3B5998;
    padding: 5px;
}

#register {
position: relative;
left: 3%;
top: 4%;   
}
.necessery {
    color: grey;
    font-size: 12px;
    padding: 5px;
}
#reg-submit {
    width: 150px !important;
    background-color: #e6e6e6;
    border: 2px solid #3B5998 !important;
    border-radius: 0px !important;
    height: 30px !important;
    margin-left: 105px !important;
}

#register input {
    display: block;
    margin-top: 10px;
    border: 1px solid #b3b3b3;
    border-radius: 2px;
    width: 35%;
    height: 22px;
    padding: 2px;
    padding-left: 8px;
}
.reg-hud {
    font-weight: 600;
    color: grey;
    font-size: 18px;
}
a {
    text-decoration: none;
}
a .f-blue {
    color: #3B5998;
}

#login {
    background-color: #3B5998;
    padding: 10px;
    position: relative;
    float:right;
    right: 15%;
    bottom: 67%;

}

.log-hud {
    color: white;
    font-weight: bold;
}
.login-div input {
    display: block;
    margin-top: 10px;
    border: 1px solid #b3b3b3;
    border-radius: 2px;
    width: 245px;
    height: 22px;
    padding: 2px;
    padding-left: 8px;
}

.login-div {
    text-align: center;
}

#checkbox  {
    float: none;
    width: auto;
    display: inline-block;
}

#checkbox-label {
    font-size:12px;
    display: inline-block;
    vertical-align: top;
    margin-top: 13px;
    color: #fff;
}



label.custom-checkbox input[type="checkbox"] {
        float:left;

    opacity:0;
}

label.custom-checkbox input[type="checkbox"] ~ .helping-el{
    background-color: #5072A7;
    border: 4px solid white;  
    border-radius: 2px;

    display: inline-block;
    padding: 10px;

    position: relative;
    top: 2px;


}

label.custom-checkbox input[type="checkbox"]:checked ~ .helping-el{
    border: 4px solid #F8F8FF;
    background-color: #5072A7;

}

label.custom-checkbox input[type="checkbox"]:checked ~ .helping-el:after {
    color: #FFFFFF;
    content: "\2714";
    font-size: 20px;
    font-weight: normal;
    left: 2px;
    position: absolute;
    top: -6px;
    transform: rotate(10deg);
}



.loginbutton{
            border: 3px solid white;
            font-weight: bold;
            background-color: #5072A7;
            width: 15%;
            height: 30px;
            position: absolute;
            right: 20px;

            color: white

}

        <div id="loginbox">
        <p id="heding">Enter the system</p>
        <p class="necessery"> It is necessery to log in Your account in order to sign in for a course.</p>
            <div id="register">
                <div class="reg-hud">
                    ARE YOU NEW? <a href="#"><span class="f-blue">REGISTER</span></a>
                </div>
                <form>
                    <input type="text" name="username" placeholder="User name">
                    <input type="email" name="email" placeholder="Email">
                    <input type="passowrd" name="pass" placeholder="Password">
                    <input type="password" name="pass" placeholder="Confirm Password">
                    <input type="submit" id="reg-submit" name="submit" value="Register">
                </form>
            </div>
            <div id="login">
                <div class="log-hud">
                    ALREADY A STUDENT? LOGIN
                </div>
                <form>
                    <div class="login-div">
                        <input type="text" name="username" placeholder="User name">
                        <input type="password" name="password" placeholder ="Password">


           <label class="custom-checkbox">
        <input type="checkbox" />
        <span class="helping-el"></span>
        <span class="span3">Remember me?</span>
        </label>

                <input type="submit" name="register" value="Login" class="loginbutton">




                    </div>
                </form>
            </div>

        </div>

My desired result is something like this :https://imgur.com/a/ICeuVUM

2 个答案:

答案 0 :(得分:1)

在第一个

label.custom-checkbox input[type="checkbox"] {
    /* float: left; */
    /* opacity: 0; */
    display: none;
}

.custom-checkbox{
    float:left;
}

并将类添加到“登录”按钮中,说出login-btn以添加宽度,如下所示:

.login-btn{
    width: 30%;
}

这是结果:

enter image description here

答案 1 :(得分:0)

好的,我在您的代码中更改了一些样式。我建议您在要对齐内容时应使用flex-box,并尝试使用更多的div来包装元素。避免过度使用重要的东西。您可以按照下面的代码结构进行操作。它的响应速度也很完美。希望能有所帮助:)

body, html, div, p {
    margin: 0px;
    padding: 0px;
}
body {
    padding: 5px;
    font-family: Verdana;
}
.container{
      background: #e6e6e6;
      padding: 20px;
}
.heading{
      margin-bottom: 20px;
}

#register {
    margin-bottom: 20px;
}

#loginbox {
    background: #e6e6e6;
    display: flex;
    justify-content: space-between;
}

#heding {
    font-family: Verdana;
    font-weight: 600;
    font-size: 22px;
    color: #3B5998;
    margin: 5px 0px;
}


.necessery {
    color: grey;
    font-size: 12px;
}
#reg-submit {
    width: 150px;
    background-color: #e6e6e6;
    border: 2px solid #3B5998;
    border-radius: 0px;
    float: right;
    height: 30px !important;
    margin: 15px 0px;
}

#register input {
    margin-top: 10px;
    border: 1px solid #b3b3b3;
    border-radius: 2px;
    width: 100%;
    /* height: 22px; */
    box-sizing: border-box;
    padding: 5px;
    padding-left: 8px;
}
.reg-hud {
    font-weight: 600;
    color: grey;
    font-size: 18px;
    width: 100%;
}
a {
    text-decoration: none;
}
a .f-blue {
    color: #3B5998;
}

#login {
   background-color: #3B5998;
    padding: 10px 20px;
    /* position: relative; */
    /* float: right; */
    /* right: 15%; */
    /* bottom: 67%; */
    max-width: 100%;
}
.footer-container{
  display:flex;
  justify-content: space-between;
  margin: 20px 0px;
}
.log-hud {
    color: white;
    font-weight: bold;
}
.login-div  input[type="text"],.login-div  input[type="password"] {
    margin-top: 20px;
    border: 1px solid #b3b3b3;
    border-radius: 2px;
    box-sizing: border-box;
    width: 100%;
    padding: 5px 0px;
    padding-left: 8px;
}

.login-div {
    text-align: center;
}

#checkbox  {
    float: none;
    width: auto;
    display: inline-block;
}

#checkbox-label {
    font-size:12px;
    display: inline-block;
    vertical-align: top;
    margin-top: 13px;
    color: #fff;
}



label.custom-checkbox input[type="checkbox"] {
    margin: 0px;
    width: 0px;
    opacity: 0;
}

label.custom-checkbox input[type="checkbox"] ~ .helping-el{
    background-color: #5072A7;
    border: 4px solid white;  
    border-radius: 2px;
    display: inline-block;
    padding: 10px;

}
.custom-checkbox{
    display: flex;
    align-items: baseline;
    justify-content: space-between;
}
.span3{
  margin: 0px 10px;
}
label.custom-checkbox input[type="checkbox"]:checked ~ .helping-el{
    border: 4px solid #F8F8FF;
    background-color: #5072A7;

}

label.custom-checkbox input[type="checkbox"]:checked ~ .helping-el:after {
    color: #FFFFFF;
    content: "\2714";
    font-size: 20px;
    font-weight: normal;
    left: 2px;
    position: absolute;
    top: -6px;
    transform: rotate(10deg);
}



.loginbutton{
    border: 3px solid white;
    font-weight: bold;
    background-color: #5072A7;
    width: 30%;
    height: 30px;
    color: white;
}

@media screen and (max-width: 768px){
  #loginbox {
    background: #e6e6e6;
    display: flex;
    flex-direction: column;
  }

}
<div class="container">
 
   <div class="heading">
    <p id="heding">Enter the system</p>
    <p class="necessery"> It is necessery to log in Your account in order to sign in for a course.</p>  
    </div>

   <div id="loginbox">
    <div id="register">
      <div class="reg-hud">
        ARE YOU NEW? <a href="#"><span class="f-blue">REGISTER</span></a>
      </div>
      <form>
        <div>
        <input type="text" name="username" placeholder="User name">        
        </div>
        <div>
        <input type="email" name="email" placeholder="Email"> 
        </div>
        <div>        
        <input type="passowrd" name="pass" placeholder="Password">
        </div>
        <div>        
        <input type="password" name="pass" placeholder="Confirm Password">
        </div>
        <button id="reg-submit" name="submit" value="Register">
          Register
        </button>
      </form>
    </div>
    <div id="login">
      <div class="log-hud">
        ALREADY A STUDENT? LOGIN
      </div>
      <form>
        <div class="login-div">
          <input type="text" name="username" placeholder="User name">
          <input type="password" name="password" placeholder ="Password">
          <div class="footer-container">
              <label class="custom-checkbox">
                  <input type="checkbox" />
                  <span class="helping-el"></span>
                  <span class="span3">Remember me?</span>
              </label>
              <button  name="submit"  class="loginbutton">
              Login
             </button>
          </div>

        </div>
      </form>
    </div>
  </div>

</div>