通常我们用到的css布局都是左右居中,经典css写法如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>登录</title>
</head>
<body>
<center>
<div style="position: absolute; top: 50%;">
<div style="position: relative; left:-50%; top:-50%; width:300px;height:200px; background:url(${pageContext.request.contextPath}/images/beijing.jpg);">
<table>
<tr>
<td>帐号:</td>
<td><input type="text" name="textfield" style="width:105px; height:17px; background-color:#292929; border:solid 1px #7dbad7; font-size:12px; color:#6cd0ff"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="textfield2" style="width:105px; height:17px; background-color:#292929; border:solid 1px #7dbad7; font-size:12px; color:#6cd0ff"></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
<td><input type="submit" value="注册"></td>
</tr>
</table>
</div>
</div>
</center>
</body>
</html>
第3中方式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<!--
<div style="width:400px; height:400px; background:#99FF00;">
<div style="width:100px; height:100px; position:relative; top:50%; left:50%;">
<div style="position:relative; margin-left:-50%; margin-top:-50%; background:#CC9900;">
asdfasdf;ajdslkas
</div>
</div>
</div>
<br/><br/>
<hr/><hr/>
<div style="width:400px; height:400px; background:#99FF00;">
<div style="position:relative; top:50%; background:#CC9900;">
<div style="background:#FF0000; width:100px; height:100px; position:relative; margin-top:-50%;">safasd</div>
</div>
</div>
-->
<div style="width:400px; height:400px; background:#99FF00;">
<div style="width:100px; height:100px; position:relative; top:50%; left:50%; margin-left:-50px; background:#FF0000;">
<div style="position:relative; margin-top:-50%; background:#CC9900;">
asdfasdf;ajdslkas
<br/>
asfasdfas
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body style="text-align: center;">
<div
style="width:300px;height:200px;background:url(beijing.jpg) no-repeat; position: absolute; top:50%;margin-top:-100px; left:50%; margin-left:-150px;">
<table align="center">
<div>
<tr>
<td>
帐号:
</td>
<td>
<input type="text" name="textfield"
style="width: 105px; height: 17px; background-color: #292929; border: solid 1px #7dbad7; font-size: 12px; color: #6cd0ff">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="textfield2"
style="width: 105px; height: 17px; background-color: #292929; border: solid 1px #7dbad7; font-size: 12px; color: #6cd0ff">
</td>
</tr>
<tr>
<td>
<input type="submit" value="登录">
</td>
<td>
<input type="submit" value="注册">
</td>
</tr>
</div>
</table>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
body{
margin:0px;
}
#container{
position:absolute;
width:100%;
background-color:#808080;
top:20%;
bottom:20%;
lex:expression(this.style.height=parseInt(document.body.clientHeight)-this.style.pixelTop*2);
}
</style>
</head>
<body>
<div id="container" style="top:20%;">
1234567890
</div>
</body>
</html>
<html>
<head>
<title>登录</title>
</head>
<body>
<table style="vertical-align:middle;width=100%;height=100%;text-align:center;">
<tr><td>
<table
style="width:300px;height:200px;text-align:center;background:url(beijing.jpg);">
<tr><td>
<table>
<tr>
<td>
帐号:
</td>
<td>
<input type="text" name="textfield"
style="width: 105px; height: 17px; background-color: #292929; border: solid 1px #7dbad7; font-size: 12px; color: #6cd0ff">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="textfield2"
style="width: 105px; height: 17px; background-color: #292929; border: solid 1px #7dbad7; font-size: 12px; color: #6cd0ff">
</td>
</tr>
<tr>
<td>
<input type="submit" value="登录">
</td>
<td>
<input type="submit" value="注册">
</td>
</tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
</body>
</html>