我正在建立一个登录表单,我想在屏幕中间的用户名和密码字段正方形。我也使用Zurb Foundation软件包来构建这个表单。我把事情放在垂直方向上有很多麻烦。
这是我的代码:
<!DOCTYPE html> <!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ --> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]--> <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]--> <!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]--> <head> <Meta charset="utf-8" /> <!-- Set the viewport width to device width for mobile --> <Meta name="viewport" content="width=device-width" /> <title> Registration </title> <!-- Included CSS Files (Compressed) --> <link rel="stylesheet" href="stylesheets/foundation.min.css"> <link rel="stylesheet" href="stylesheets/app.css"> <link rel="stylesheet" href="stylesheets/main.css"> <script src="javascripts/modernizr.foundation.js"></script> <!-- IE Fix for HTML5 Tags --> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <nav class="top-bar"> <ul> <li class="name"><h1><a href="#">Title</a></h1></li> <li class="toggle-topbar"><a href="#"></a></li> </ul> <section> <ul class="left"> <li><a href="#">Link</a></li> </ul> <ul class="right"> <li><a href="#">Link</a></li> </ul> </section> </nav> <body> <div class="row" id="parent"> <div class="six columns" id="child"> <form id = "register"> <input type="text"> </form> </div> </div> <script src="javascripts/jquery.foundation.topbar.js"></script> <!-- Included JS Files (Compressed) --> <script src="javascripts/jquery.js"></script> <script src="javascripts/foundation.min.js"></script> <!-- Initialize JS Plugins --> <script src="javascripts/app.js"></script> </body> </html>
和一些CSS,我已经玩了,但还没有工作了:
body { /*background-image: url('../images/gplaypattern.png');*/ background: red; } #register { background-color: black; width:80%; } #parent { position: absolute; left: 50%; top: 50%; height: 80px; } #child { height: 75px; position:absolute; top: 50%; background: green; }
如果我把东西的高度改变为动态(即高度:30%;),它不起作用,为什么呢?
解决方法
UPDATE
该功能作为组件(xy中心)包含在即将到来的Foundation版本中。 Github更多细节。
如果您使用的是旧版本的Foundation,则使用CSS Tricks centering method将以最小量的CSS / HTML标记垂直和水平居中登录表单。
HTML
<div class="row" > <div class="small-12 medium-6 columns" id="login"> <form > <div class="row collapse"> <div class="small-12 medium-5 columns"> <input type="text" name="username" placeholder="Username" /> </div> <div class="small-12 medium-5 columns"> <input type="password" name="password" placeholder="Password" /> </div> <div class="small-12 medium-2 small-centered medium-uncentered columns"> <a href="#" class="button postfix">Go</a> </div> </div> </form> </div> </div>
CSS
#login { position: absolute; left: 50%; top: 50%; /* * Where the magic happens * Centering method from CSS Tricks * http://css-tricks.com/centering-percentage-widthheight-elements/ */ transform: translate(-50%,-50%); -webkit-transform: translate(-50%,-50%); -o-transform: translate(-50%,-50%); -ms-transform: translate(-50%,-50%); }
结果
登录表单将保持垂直和水平中心,无论屏幕大小如何。这是结果的屏幕截图。
和工作jsFiddle