【问题标题】:How to move form from center to right如何将表格从中心移动到右侧
【发布时间】:2017-02-03 12:52:02
【问题描述】:

我正在 CodeIgniter 上处理这个登录/注册表单。 从 Internet 获得此登录/注册代码,但默认情况下,它位于中心。那么如何将这个表单移动到页面的右侧呢? enter image description here

登录.html

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Sign-Up/Login Form</title>
  <link href='http://fonts.googleapis.com/css?family=Titillium+Web:400,300,600' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
      <link rel="stylesheet" href="<?php echo base_url('resources/style.css');?>">

  
</head>

<body>
  <div class="form">
      
      <ul class="tab-group">
        <li class="tab active"><a href="#signup">Sign Up</a></li>
        <li class="tab"><a href="#login">Log In</a></li>
      </ul>
      
      <div class="tab-content">
        <div id="signup">   
          <h1>Sign Up for Free</h1>
          
          <form action="/" method="post">
          
          <div class="top-row">
            <div class="field-wrap">
              <label>
                First Name<span class="req">*</span>
              </label>
              <input type="text" required autocomplete="off" />
            </div>
        
            <div class="field-wrap">
              <label>
                Last Name<span class="req">*</span>
              </label>
              <input type="text"required autocomplete="off"/>
            </div>
          </div>

          <div class="field-wrap">
            <label>
              Email Address<span class="req">*</span>
            </label>
            <input type="email"required autocomplete="off"/>
          </div>
          
          <div class="field-wrap">
            <label>
              Set A Password<span class="req">*</span>
            </label>
            <input type="password"required autocomplete="off"/>
          </div>
          
          <button type="submit" class="button button-block">Get Started</button>
          
          </form>

        </div>
        
        <div id="login">   
          <h1>Welcome Back!</h1>
          
          <form action="/" method="post">
          
            <div class="field-wrap">
            <label>
              Email Address<span class="req">*</span>
            </label>
            <input type="email"required autocomplete="off"/>
          </div>
          
          <div class="field-wrap">
            <label>
              Password<span class="req">*</span>
            </label>
            <input type="password"required autocomplete="off"/>
          </div>
          
          <p class="forgot"><a href="#">Forgot Password?</a></p>
          
          <button class="button button-block">Log In</button>
          
          </form>

        </div>
        
      </div><!-- tab-content -->
      
</div> <!-- /form -->
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

    <script src="<?php echo base_url('resources/index.js');?>"></script>

</body>
</html>

样式.css

*, *:before, *:after {
  box-sizing: border-box;
}

html {
  overflow-y: scroll;
}

body {
  background: #c1bdba;
  font-family: 'Titillium Web', sans-serif;
}

a {
  text-decoration: none;
  color: #1ab188;
  -webkit-transition: .5s ease;
  transition: .5s ease;
}
a:hover {
  color: #179b77;
}

.form {
  background: rgba(19, 35, 47, 0.9);
  padding: 40px;
  max-width: 600px;
  margin: 40px auto;
  border-radius: 4px;
  box-shadow: 0 4px 10px 4px rgba(19, 35, 47, 0.3);
}

.tab-group {
  list-style: none;
  padding: 0;
  margin: 0 0 40px 0;
}
.tab-group:after {
  content: "";
  display: table;
  clear: both;
}
.tab-group li a {
  display: block;
  text-decoration: none;
  padding: 15px;
  background: rgba(160, 179, 176, 0.25);
  color: #a0b3b0;
  font-size: 20px;
  float: left;
  width: 50%;
  text-align: center;
  cursor: pointer;
  -webkit-transition: .5s ease;
  transition: .5s ease;
}
.tab-group li a:hover {
  background: #179b77;
  color: #ffffff;
}
.tab-group .active a {
  background: #1ab188;
  color: #ffffff;
}

.tab-content > div:last-child {
  display: none;
}

h1 {
  text-align: center;
  color: #ffffff;
  font-weight: 300;
  margin: 0 0 40px;
}

label {
  position: absolute;
  -webkit-transform: translateY(6px);
          transform: translateY(6px);
  left: 13px;
  color: rgba(255, 255, 255, 0.5);
  -webkit-transition: all 0.25s ease;
  transition: all 0.25s ease;
  -webkit-backface-visibility: hidden;
  pointer-events: none;
  font-size: 22px;
}
label .req {
  margin: 2px;
  color: #1ab188;
}

label.active {
  -webkit-transform: translateY(50px);
          transform: translateY(50px);
  left: 2px;
  font-size: 14px;
}
label.active .req {
  opacity: 0;
}

label.highlight {
  color: #ffffff;
}

input, textarea {
  font-size: 22px;
  display: block;
  width: 100%;
  height: 100%;
  padding: 5px 10px;
  background: none;
  background-image: none;
  border: 1px solid #a0b3b0;
  color: #ffffff;
  border-radius: 0;
  -webkit-transition: border-color .25s ease, box-shadow .25s ease;
  transition: border-color .25s ease, box-shadow .25s ease;
}
input:focus, textarea:focus {
  outline: 0;
  border-color: #1ab188;
}

textarea {
  border: 2px solid #a0b3b0;
  resize: vertical;
}

.field-wrap {
  position: relative;
  margin-bottom: 40px;
}

.top-row:after {
  content: "";
  display: table;
  clear: both;
}
.top-row > div {
  float: left;
  width: 48%;
  margin-right: 4%;
}
.top-row > div:last-child {
  margin: 0;
}

.button {
  border: 0;
  outline: none;
  border-radius: 0;
  padding: 15px 0;
  font-size: 2rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .1em;
  background: #1ab188;
  color: #ffffff;
  -webkit-transition: all 0.5s ease;
  transition: all 0.5s ease;
  -webkit-appearance: none;
}
.button:hover, .button:focus {
  background: #179b77;
}

.button-block {
  display: block;
  width: 100%;
}

.forgot {
  margin-top: -20px;
  text-align: right;
}

index.js

$('.form').find('input, textarea').on('keyup blur focus', function (e) {
  
  var $this = $(this),
      label = $this.prev('label');

	  if (e.type === 'keyup') {
			if ($this.val() === '') {
          label.removeClass('active highlight');
        } else {
          label.addClass('active highlight');
        }
    } else if (e.type === 'blur') {
    	if( $this.val() === '' ) {
    		label.removeClass('active highlight'); 
			} else {
		    label.removeClass('highlight');   
			}   
    } else if (e.type === 'focus') {
      
      if( $this.val() === '' ) {
    		label.removeClass('highlight'); 
			} 
      else if( $this.val() !== '' ) {
		    label.addClass('highlight');
			}
    }

});

$('.tab a').on('click', function (e) {
  
  e.preventDefault();
  
  $(this).parent().addClass('active');
  $(this).parent().siblings().removeClass('active');
  
  target = $(this).attr('href');

  $('.tab-content > div').not(target).hide();
  
  $(target).fadeIn(600);
  
});

【问题讨论】:

  • CSS 中使用 right:0px; 这会有所帮助

标签: javascript html css codeigniter


【解决方案1】:

试试这个。这将起作用

form {
  margin-left: 150px; // Change the number as you want
}

【讨论】:

    【解决方案2】:

    将 float: 添加到您的内容。在您的 css 更改表单类中,如下所示

     .form {
        float: right;
      background: rgba(19, 35, 47, 0.9);
      padding: 40px;
      max-width: 600px;
      margin: 40px auto;
      border-radius: 4px;
      box-shadow: 0 4px 10px 4px rgba(19, 35, 47, 0.3);
        margin-right: 20px; /*what ever you want*/
    }
    

    【讨论】:

    • 工作了,但是我想在表格和右上角之间留一点空间怎么做?
    • @AkhilBiju 检查我编辑的答案。如果有用的话。然后接受/投票
    • 非常感谢!
    • 这不是问题。只是因为登录表单中有单个字段
    【解决方案3】:

    您可以简单地向表单类添加一个 float 属性,以及 20 或 30 像素的 margin-left。

    .form {
        float: right;
        margin-left: 30px;
    }
    

    只需将这两个添加到您的 .form 类中即可。

    【讨论】:

    • 我不希望它完全在右端,我怎么能留一点空间?
    • 嗯...对不起。我想我把事情搞砸了。试试 margin-right 而不是 margin-left。
    【解决方案4】:
    Working fine!
    
    
        $('.form').find('input, textarea').on('keyup blur focus', function (e) {
          
          var $this = $(this),
              label = $this.prev('label');
    
        	  if (e.type === 'keyup') {
        			if ($this.val() === '') {
                  label.removeClass('active highlight');
                } else {
                  label.addClass('active highlight');
                }
            } else if (e.type === 'blur') {
            	if( $this.val() === '' ) {
            		label.removeClass('active highlight'); 
        			} else {
        		    label.removeClass('highlight');   
        			}   
            } else if (e.type === 'focus') {
              
              if( $this.val() === '' ) {
            		label.removeClass('highlight'); 
        			} 
              else if( $this.val() !== '' ) {
        		    label.addClass('highlight');
        			}
            }
    
        });
    
        $('.tab a').on('click', function (e) {
          
          e.preventDefault();
          
          $(this).parent().addClass('active');
          $(this).parent().siblings().removeClass('active');
          
          target = $(this).attr('href');
    
          $('.tab-content > div').not(target).hide();
          
          $(target).fadeIn(600);
          
        });
        *, *:before, *:after {
          box-sizing: border-box;
        }
    
        html {
          overflow-y: scroll;
        }
    
        body {
          background: #c1bdba;
          font-family: 'Titillium Web', sans-serif;
        }
    
        a {
          text-decoration: none;
          color: #1ab188;
          -webkit-transition: .5s ease;
          transition: .5s ease;
        }
        a:hover {
          color: #179b77;
        }
    
        .form {
          background: rgba(19, 35, 47, 0.9);
          padding: 40px;
          max-width:50%;
          margin: 40px auto;
          border-radius: 4px;
    float:right;
          box-shadow: 0 4px 10px 4px rgba(19, 35, 47, 0.3);
        }
    
        .tab-group {
          list-style: none;
          padding: 0;
          margin: 0 0 40px 0;
        }
        .tab-group:after {
          content: "";
          display: table;
          clear: both;
        }
        .tab-group li a {
          display: block;
          text-decoration: none;
          padding: 15px;
          background: rgba(160, 179, 176, 0.25);
          color: #a0b3b0;
          font-size: 20px;
          float: none;
          width: 100%;
          text-align: center;
          cursor: pointer;
          -webkit-transition: .5s ease;
          transition: .5s ease;
        }
        .tab-group li a:hover {
          background: #179b77;
          color: #ffffff;
        }
        .tab-group .active a {
          background: #1ab188;
          color: #ffffff;
        }
    
        .tab-content > div:last-child {
          display: none;
        }
    
        h1 {
          text-align: center;
          color: #ffffff;
          font-weight: 300;
          margin: 0 0 40px;
        }
    input{height:45px;}
    
        label {
          position: absolute;
          -webkit-transform: translateY(6px);
                  transform: translateY(6px);
          left: 13px;
          color: rgba(255, 255, 255, 0.5);
          -webkit-transition: all 0.25s ease;
          transition: all 0.25s ease;
          -webkit-backface-visibility: hidden;
          pointer-events: none;
          font-size: 22px;
        }
        label .req {
          margin: 2px;
          color: #1ab188;
        }
    
        label.active {
          -webkit-transform: translateY(50px);
                  transform: translateY(50px);
          left: 2px;
          font-size: 14px;
        }
        label.active .req {
          opacity: 0;
        }
    
        label.highlight {
          color: #ffffff;
        }
    
        input, textarea {
          font-size: 22px;
          display: block;
          width: 100%;
          height: 100%;
          padding: 5px 10px;
          background: none;
          background-image: none;
          border: 1px solid #a0b3b0;
          color: #ffffff;
          border-radius: 0;
          -webkit-transition: border-color .25s ease, box-shadow .25s ease;
          transition: border-color .25s ease, box-shadow .25s ease;
        }
        input:focus, textarea:focus {
          outline: 0;
          border-color: #1ab188;
        }
    
        textarea {
          border: 2px solid #a0b3b0;
          resize: vertical;
        }
    
        .field-wrap {
          position: relative;
          margin-bottom: 40px;
        }
    
        .top-row:after {
          content: "";
          display: table;
          clear: both;
        }
        .top-row > div {
          float: left;
          width: 48%;
          margin-right: 4%;
        }
        .top-row > div:last-child {
          margin: 0;
        }
    
        .button {
          border: 0;
          outline: none;
          border-radius: 0;
          padding: 15px 0;
          font-size: 2rem;
          font-weight: 600;
          text-transform: uppercase;
          letter-spacing: .1em;
          background: #1ab188;
          color: #ffffff;
          -webkit-transition: all 0.5s ease;
          transition: all 0.5s ease;
          -webkit-appearance: none;
        }
        .button:hover, .button:focus {
          background: #179b77;
        }
    
        .button-block {
          display: block;
          width: 100%;
        }
    
        .forgot {
          margin-top: -20px;
          text-align: right;
        }
        <!DOCTYPE html>
        <html >
        <head>
          <meta charset="UTF-8">
          <title>Sign-Up/Login Form</title>
          <link href='http://fonts.googleapis.com/css?family=Titillium+Web:400,300,600' rel='stylesheet' type='text/css'>
          <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
    
          
              <link rel="stylesheet" href="<?php echo base_url('resources/style.css');?>">
    
          
        </head>
    
        <body>
          <div class="form">
              
              <ul class="tab-group">
                <li class="tab active"><a href="#signup">Sign Up</a></li>
                <li class="tab"><a href="#login">Log In</a></li>
              </ul>
              
              <div class="tab-content">
                <div id="signup">   
                  <h1>Sign Up for Free</h1>
                  
                  <form action="/" method="post">
                  
                  <div class="top-row">
                    <div class="field-wrap">
                      <label>
                        First Name<span class="req">*</span>
                      </label>
                      <input type="text" required autocomplete="off" />
                    </div>
                
                    <div class="field-wrap">
                      <label>
                        Last Name<span class="req">*</span>
                      </label>
                      <input type="text"required autocomplete="off"/>
                    </div>
                  </div>
    
                  <div class="field-wrap">
                    <label>
                      Email Address<span class="req">*</span>
                    </label>
                    <input type="email"required autocomplete="off"/>
                  </div>
                  
                  <div class="field-wrap">
                    <label>
                      Set A Password<span class="req">*</span>
                    </label>
                    <input type="password"required autocomplete="off"/>
                  </div>
                  
                  <button type="submit" class="button button-block">Get Started</button>
                  
                  </form>
    
                </div>
                
                <div id="login">   
                  <h1>Welcome Back!</h1>
                  
                  <form action="/" method="post">
                  
                    <div class="field-wrap">
                    <label>
                      Email Address<span class="req">*</span>
                    </label>
                    <input type="email"required autocomplete="off"/>
                  </div>
                  
                  <div class="field-wrap">
                    <label>
                      Password<span class="req">*</span>
                    </label>
                    <input type="password"required autocomplete="off"/>
                  </div>
                  
                  <p class="forgot"><a href="#">Forgot Password?</a></p>
                  
                  <button class="button button-block">Log In</button>
                  
                  </form>
    
                </div>
                
              </div><!-- tab-content -->
              
        </div> <!-- /form -->
          <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    
            <script src="<?php echo base_url('resources/index.js');?>"></script>
    
        </body>
        </html>

    【讨论】:

    • @Akhil biju 立即查看
    • @BrianRamsey 检查一下
    • @HasmukhBaldaniya 当我点击登录时,表单变小了。看这里 prntscr.com/e401mr 你知道我怎样才能保持表单大小不变吗?
    • @AkhilBiju 是的我知道
    【解决方案5】:

    这是你需要的吗?

    <!DOCTYPE html>
    <html >
    <head>
      <meta charset="UTF-8">
      <title>Sign-Up/Login Form</title>
      <link href='http://fonts.googleapis.com/css?family=Titillium+Web:400,300,600' rel='stylesheet' type='text/css'>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
    
      
          <link rel="stylesheet" href="<?php echo base_url('resources/style.css');?>">
    
      
    </head>
    
    <body>
      <div class="form">
          
          <ul class="tab-group">
            <li class="tab active"><a href="#signup">Sign Up</a></li>
            <li class="tab"><a href="#login">Log In</a></li>
          </ul>
          
          <div class="tab-content" style="float:right;">
            <div id="signup">   
              <h1>Sign Up for Free</h1>
              
              <form action="/" method="post">
              
              <div class="top-row">
                <div class="field-wrap">
                  <label>
                    First Name<span class="req">*</span>
                  </label>
                  <input type="text" required autocomplete="off" />
                </div>
            
                <div class="field-wrap">
                  <label>
                    Last Name<span class="req">*</span>
                  </label>
                  <input type="text"required autocomplete="off"/>
                </div>
              </div>
    
              <div class="field-wrap">
                <label>
                  Email Address<span class="req">*</span>
                </label>
                <input type="email"required autocomplete="off"/>
              </div>
              
              <div class="field-wrap">
                <label>
                  Set A Password<span class="req">*</span>
                </label>
                <input type="password"required autocomplete="off"/>
              </div>
              
              <button type="submit" class="button button-block">Get Started</button>
              
              </form>
    
            </div>
            
            <div id="login">   
              <h1>Welcome Back!</h1>
              
              <form action="/" method="post">
              
                <div class="field-wrap">
                <label>
                  Email Address<span class="req">*</span>
                </label>
                <input type="email"required autocomplete="off"/>
              </div>
              
              <div class="field-wrap">
                <label>
                  Password<span class="req">*</span>
                </label>
                <input type="password"required autocomplete="off"/>
              </div>
              
              <p class="forgot"><a href="#">Forgot Password?</a></p>
              
              <button class="button button-block">Log In</button>
              
              </form>
    
            </div>
            
          </div><!-- tab-content -->
          
    </div> <!-- /form -->
      <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    
        <script src="<?php echo base_url('resources/index.js');?>"></script>
    
    </body>
    </html>

    *, *:before, *:after {
      box-sizing: border-box;
    }
    
    html {
      overflow-y: scroll;
    }
    
    body {
      background: #c1bdba;
      font-family: 'Titillium Web', sans-serif;
    }
    
    a {
      text-decoration: none;
      color: #1ab188;
      -webkit-transition: .5s ease;
      transition: .5s ease;
    }
    a:hover {
      color: #179b77;
    }
    
    .form {
      background: rgba(19, 35, 47, 0.9);
      padding: 40px;
      max-width: 600px;
      margin: 40px auto;
      border-radius: 4px;
      box-shadow: 0 4px 10px 4px rgba(19, 35, 47, 0.3);
    }
    
    .tab-group {
      list-style: none;
      padding: 0;
      margin: 0 0 40px 0;
    }
    .tab-group:after {
      content: "";
      display: table;
      clear: both;
    }
    .tab-group li a {
      display: block;
      text-decoration: none;
      padding: 15px;
      background: rgba(160, 179, 176, 0.25);
      color: #a0b3b0;
      font-size: 20px;
      float: left;
      width: 50%;
      text-align: center;
      cursor: pointer;
      -webkit-transition: .5s ease;
      transition: .5s ease;
    }
    .tab-group li a:hover {
      background: #179b77;
      color: #ffffff;
    }
    .tab-group .active a {
      background: #1ab188;
      color: #ffffff;
    }
    
    .tab-content > div:last-child {
      display: none;
    }
    
    h1 {
      text-align: center;
      color: #ffffff;
      font-weight: 300;
      margin: 0 0 40px;
    }
    
    label {
      position: absolute;
      -webkit-transform: translateY(6px);
              transform: translateY(6px);
      left: 13px;
      color: rgba(255, 255, 255, 0.5);
      -webkit-transition: all 0.25s ease;
      transition: all 0.25s ease;
      -webkit-backface-visibility: hidden;
      pointer-events: none;
      font-size: 22px;
    }
    label .req {
      margin: 2px;
      color: #1ab188;
    }
    
    label.active {
      -webkit-transform: translateY(50px);
              transform: translateY(50px);
      left: 2px;
      font-size: 14px;
    }
    label.active .req {
      opacity: 0;
    }
    
    label.highlight {
      color: #ffffff;
    }
    
    input, textarea {
      font-size: 22px;
      display: block;
      width: 100%;
      height: 100%;
      padding: 5px 10px;
      background: none;
      background-image: none;
      border: 1px solid #a0b3b0;
      color: #ffffff;
      border-radius: 0;
      -webkit-transition: border-color .25s ease, box-shadow .25s ease;
      transition: border-color .25s ease, box-shadow .25s ease;
    }
    input:focus, textarea:focus {
      outline: 0;
      border-color: #1ab188;
    }
    
    textarea {
      border: 2px solid #a0b3b0;
      resize: vertical;
    }
    
    .field-wrap {
      position: relative;
      margin-bottom: 40px;
    }
    
    .top-row:after {
      content: "";
      display: table;
      clear: both;
    }
    .top-row > div {
      float: left;
      width: 48%;
      margin-right: 4%;
    }
    .top-row > div:last-child {
      margin: 0;
    }
    
    .button {
      border: 0;
      outline: none;
      border-radius: 0;
      padding: 15px 0;
      font-size: 2rem;
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: .1em;
      background: #1ab188;
      color: #ffffff;
      -webkit-transition: all 0.5s ease;
      transition: all 0.5s ease;
      -webkit-appearance: none;
    }
    .button:hover, .button:focus {
      background: #179b77;
    }
    
    .button-block {
      display: block;
      width: 100%;
    }
    
    .forgot {
      margin-top: -20px;
      text-align: right;
    }

    $('.form').find('input, textarea').on('keyup blur focus', function (e) {
      
      var $this = $(this),
          label = $this.prev('label');
    
    	  if (e.type === 'keyup') {
    			if ($this.val() === '') {
              label.removeClass('active highlight');
            } else {
              label.addClass('active highlight');
            }
        } else if (e.type === 'blur') {
        	if( $this.val() === '' ) {
        		label.removeClass('active highlight'); 
    			} else {
    		    label.removeClass('highlight');   
    			}   
        } else if (e.type === 'focus') {
          
          if( $this.val() === '' ) {
        		label.removeClass('highlight'); 
    			} 
          else if( $this.val() !== '' ) {
    		    label.addClass('highlight');
    			}
        }
    
    });
    
    $('.tab a').on('click', function (e) {
      
      e.preventDefault();
      
      $(this).parent().addClass('active');
      $(this).parent().siblings().removeClass('active');
      
      target = $(this).attr('href');
    
      $('.tab-content > div').not(target).hide();
      
      $(target).fadeIn(600);
      
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2019-08-09
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多