<section>
<form id="" name="" method="post" action="">
<label for="username">User:</label>
<input type="text" id="username" name="username" />
<label for="password">Password:</label>
<input type="password" id="password" name="password" />
<input type="submit" id="login" name="login" value="Entrar" />
</form>
</section>
section {
width: 300px;
height: auto;
margin: 0 auto;
position: relative;
top: 250px; /* pixels from top of page - if you have the height to be auto, may see different changes, however, this could be what you want if you want the form to be in the exact center -- simply change the px's to suit your needs */
}
这是你要问的吗?演示:http: //jsfiddle.net/zyFG5/
编辑:
好的,试一试:
<section>
<form id="" name="" method="post" action="">
<label for="username">User:</label>
<input type="text" id="username" name="username" />
<label for="password">Password:</label>
<input type="password" id="password" name="password" />
<input type="submit" id="login" name="login" value="Entrar" />
</form>
</section>
section {
width: 300px;
height: 300px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -150px;
}
负边距正好是高度和宽度的一半,它将元素拉回到完美的中心。仅适用于固定高度/宽度的元素。
演示:http: //jsfiddle.net/DDZmY/