豆猫
2009-05-13 17:08
最外的容器要固定高度,内容多行垂直居中。
代码:
<!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">
p{
margin: 0;
}
body{
font-size: 12px;
font-family: "宋体";
}
.main{
width:230px;
height:100px;
border:1px solid #777;
text-align:center;
display:table-cell;
vertical-align:middle;
color:#f00;
position: relative;
}
/*FOR IE*/
.main div {
* width: 230px;
*position:absolute;
*top:50%;
*left:50%;
}
.main div p {
*position:relative;
*top:-50%;
*left:-50%;
background:#ff0;
}
-->
</style>
</head>
<body>
<div class="main">
<div><p>当我有一行的时候会垂直居中,当我有一行的时候会垂直居中当我有一行的时候会垂直居中当我有一行的时候会垂直居中</p></div>
</div>
</body>
</html>
<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">
p{
margin: 0;
}
body{
font-size: 12px;
font-family: "宋体";
}
.main{
width:230px;
height:100px;
border:1px solid #777;
text-align:center;
display:table-cell;
vertical-align:middle;
color:#f00;
position: relative;
}
/*FOR IE*/
.main div {
* width: 230px;
*position:absolute;
*top:50%;
*left:50%;
}
.main div p {
*position:relative;
*top:-50%;
*left:-50%;
background:#ff0;
}
-->
</style>
</head>
<body>
<div class="main">
<div><p>当我有一行的时候会垂直居中,当我有一行的时候会垂直居中当我有一行的时候会垂直居中当我有一行的时候会垂直居中</p></div>
</div>
</body>
</html>