2016-07-13 62 views
1

我的页面上有很多内容,还有一个显示用户何时单击按钮的窗体。我可以用Flexbox的像这样居中形式:用flexbox居中放置表单元素

.container-column { 
    display: flex; 
    flex-wrap: wrap; 
    flex-direction: column; 
    align-items: center; 
} 

这中心的形式,但这种方法的问题是,它推动我的内容的其余部分下降,这是视觉上不美观。我试图添加999的z-index,但这不起作用。我也使用了position:absolute,但这只是将表格放在左上角。

是否有一种简单的方式将表单放在页面中间,而将其余内容保留原样?我对覆盖其余内容的表单很满意。

更新1根据要求,这里是有关的代码:

Form

return (
      <form className="container-column"> 
       <div className="icon ion-ios-close-empty close-btn" onClick={() => this.props.signupBtnClick('close')}></div> 
       <input type="text" name="firstname" placeholder="firstname" className="form-element"></input> 
       <input type="text" name="lastname" placeholder="lastname" className="form-element"></input> 
       <input type="text" name="username" placeholder="username" className="form-element"></input> 
       <input type="password" placeholder="password" className="form-element"></input> 
       <input type="password" placeholder="repeat password" className="form-element"></input> 
       <input type="email" name="email" placeholder="email" className="form-element"></input> 
       <input type="submit" value="submit" className="form-element btn-form2"></input> 
      </form> 
     ) 

Form是其在index页如下使用一个反应组分:

return (
      <div> 
       <section className="section-header"> 
        <div className="container-row signup-login-btns"> 
         <button className="btn btn-form1" onClick={() => this.renderLoginForm()}>Login</button> 
         <button className="btn btn-form2" onClick={() => this.renderSignupForm()}>Signup</button> 
        </div> 
        {this.state.showSignupForm ? <SignupForm signupBtnClick={(type) => this.onSignupClick(type)} />: null } 
    **other content, which gets pushed down once the form is shown** 

相关的CSS类:

.form-element { 
    border:none; 
    align-content:center; 
    text-align:center; 
    outline:none; 
    width: 400px; 
    height: 45px; 
    margin: 10px; 
    color: #7f8c8d; 
    font-size:120%; 
    border-radius:10px; 
} 

.section-header { 
    background: #d53369; 
    /* fallback for old browsers */ 
    background: -webkit-linear-gradient(to left, #d53369, #cbad6d); 
    /* Chrome 10-25, Safari 5.1-6 */ 
    background: linear-gradient(to left, #d53369, #cbad6d); 
    margin-top:0px; 
    padding-top:0px; 
    padding-bottom:5%; 

} 

回答

1

试试这个:

body { 
    position: relative; 
} 

.container-column { 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    transform: translate(-50%,-50%); 

    display: flex; 
    flex-wrap: wrap; 
    flex-direction: column; 
    align-items: center; 
} 

有关此定心方法的详细信息,请参阅:Element will not stay centered, especially when re-sizing screen

+1

这解决了它!非常感谢 (: – Frosty619