<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="cache-control" content="no-cache" />
<style>
<!--
html, body {
height: 100%;
margin: 0;
padding: 0;
}
body {
position: relative;
}
#div {
background: blue;
color: #fff;
position: absolute;
top: 50%;
left: 50%;
height: 240px;
width: 240px;
margin: -120px 0 0 -120px;
}
--></style>
</head>
<body>
<div id="div">
i'm Mr. Middle.
</div>
</body>
</html>
方法二
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="cache-control" content="no-cache" />
<style>
<!--
html, body {
height: 100%;
margin: 0 auto;
padding: 0;
}
body {
position: relative;
display: table;
}
#wrapper {
display: table-cell;
vertical-align: middle;
text-align: middle;
}
#div {
background: blue;
color: #fff;
}
* html #wrapper, *+html #wrapper {
position: absolute;
top: 50%;
}
* html #div, *+html #div {
position: relative;
top: -50%;
}
--></style>
</head>
<body>
<div id="wrapper">
<div id="div">
i'm Mr. Middle.
i'm Mr. Middle.
i'm Mr. Middle.
</div>
</div>
</body>
</html>