垂直对齐中间形式就像文本一样

时间:2014-08-28 23:18:09

标签: html css vertical-alignment

我想垂直对齐我的表单,就像我在网站上垂直对齐文字一样。但是它不起作用,我该怎么办?

enter image description here

我希望它能像这样:

enter image description here

这是我的代码http://pastebin.com/FuCNwM6e

<head>
<!DOCTYPE html>
<html>
<head>
<title>2 Column CSS Layout</title>
<style type="text/css">
body {
    margin:20px;
    font-family:"Lucida Grande","Lucida Sans Unicode","Lucida Sans",Geneva,Verdana,sans-serif;
    background-color:#555
}

div {
    text-align:center
}

#page {
    background-color:#fff;
    margin:0 auto;
    text-align:left;
    width:755px;
    padding:0 10px
}

#header {
    border-bottom:0px solid #000;
    height:30px;
    line-height:30px
}

/* Begin Navigation Bar Styling */
#nav {
    text-align:center;
    line-height: 30px;
}

#nav ul {
    text-align:center;
    font-size:11px;
    width:100%;
    margin:0;
    padding:0;
    display:inline-block;
    list-style:none;
    background-color:#f2f2f2;
    border-bottom:1px solid #ccc;
    border-top:1px solid #ccc
}

#nav li {
    margin:auto;
    display:inline-block
}

#nav li a {
    display:block;
    padding:8px 15px;
    text-decoration:none;
    font-weight:700;
    color:#069;
    border-right:1px solid #ccc
}

#nav li a:hover {
    color:#c00;
    background-color:#fff
}

/* End navigation bar styling. */

#content {
    height: 200px;
    width:100%;
    display: table;
    background:none repeat scroll 0 0 none;
}

#content p {
  display: table-cell;
  vertical-align: middle;
}

#footer {
    height:30px;
    line-height:30px;
    border-top: 1px solid #000;
    font-size:9pt
}

#footer p {
float: right;
}

.clear {
    clear: both;
}



</style>
</head>

<body>
    <div id="page">
        <div id="header">
            <div style="float: left;">
Please Login
            </div>
        </div>


        <div id="content">
<p>
<form action="" method="post">    
<label for="un">Username</label>
  <input type="text" id="un" placeholder="username" /><br/>
    <label for="pw">Password</label>
  <input type="password" id="pw" placeholder="password" /><br/>
  <input type="submit" value="Login"/>
</form>
</p>  
        </div>

        <div id="footer">
<div style="float: right;"> copyright 2014 </div>
        </div>
    </div>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

你可以这样做:

<form>放在<div>内,并为其设为#myForm

<div id="myForm">
  <p>
    <form action="" method="post">
      <label for="un">Username</label>
      <input type="text" id="un" placeholder="username" />
      <br/>
      <label for="pw">Password</label>
      <input type="password" id="pw" placeholder="password" />
      <br/>
      <input type="submit" value="Login" />
    </form>
  </p>
</div>

在CSS中使用top: __%以您想要的方式调整对齐方式:

#myForm {
    position: absolute;
    top: 16%;
    left: 42%;
}
小提琴上的

Demo [已编辑]

答案 1 :(得分:1)

像这样更改你的CSS(我为可视化添加了500px高度)

body {
    margin:20px;
    font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Geneva, Verdana, sans-serif;
    background-color:#555;
    height:100%;
}
div {
    text-align:center
}
#page {
    background-color:#fff;
    margin:0 auto;
    text-align:left;
    width:755px;
    padding:0 10px;
}
#header {
    border-bottom:0px solid #000;
    height:30px;
    line-height:30px
}
/* Begin Navigation Bar Styling */
#nav {
    text-align:center;
    line-height: 30px;
}
#nav ul {
    text-align:center;
    font-size:11px;
    width:100%;
    margin:0;
    padding:0;
    display:inline-block;
    list-style:none;
    background-color:#f2f2f2;
    border-bottom:1px solid #ccc;
    border-top:1px solid #ccc
}
#nav li {
    margin:auto;
    display:inline-block
}
#nav li a {
    display:block;
    padding:8px 15px;
    text-decoration:none;
    font-weight:700;
    color:#069;
    border-right:1px solid #ccc
}
#nav li a:hover {
    color:#c00;
    background-color:#fff
}
/* End navigation bar styling. */
#content {
    height: 500px;
    width:100%;
    background:none repeat scroll 0 0 none;
    display:flex;
    align-items:center;
}
#content p {
    display: table-cell;
    vertical-align: middle;
}
#footer {
    height:30px;
    line-height:30px;
    border-top: 1px solid #000;
    font-size:9pt
}
#content:before {
    content:'';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
    margin-right: -0.25em;
    /* Adjusts for spacing */
}
#footer p {
    float: right;
}
.clear {
    clear: both;
}
form {
    margin:0 auto;
    width: 300px;
    padding: 10px 15px;
    border: #a0a0a0 solid 1px;
    background: #f5f5f5;
}

See fiddle here

通过使用flex显示,我们可以在保持响应性的同时以任何可能的方式对齐元素,因此这将适用于固定和响应方法,无关紧要

答案 2 :(得分:0)

使用此CSS在form -

的中间垂直对齐#content
#content form {
    display: table-cell;
    vertical-align: middle;
}

<强> DEMO