怎么设定一个div垂直和水平都居中在css样式中

越简单越好

<div class="frame">
<div class="outer">
<div id="middle">
<div id="inner">图片与文本内容混合<br><img src="http://desk.blueidea.com/QTZY/GIF/xsgs_21/xsgs_21004.gif" alt="" /><br>图片与文本内容混合</div>
</div>
</div>
</div>

样式:.frame{
float:left;
margin:2px;
}
.outer {
height: 250px;
width: 240px;
overflow: hidden;
background:gold;
position: static !important;
position: relative;
display: table !important;
}
#middle { /* for explorer only*/
position: absolute;
top: 50%;
}
#middle[id] {
display: table-cell;
vertical-align: middle;
position: static;
}
#inner { /* for explorer only */
position: relative;
top: -50%;
width: 100%;
margin: 0 auto;
text-align:center
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-30
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<html xmlns="

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.aaa{ width:300px; height:100px; background:#00CC66; line-height:100px; text-align:center;}

</style>
<title>景安网络</title>
</head>

<body>
<div class="aaa">景安网络</div>
</body>
</html>

 我们可以使用text-align:center; line-height;属性设置器水平居中和垂直居中

本回答被网友采纳
第2个回答  2010-04-09
水平居中可以,垂直居中的话就麻烦了

整个容器水平居中margin:0 auto;/*上下0 左右自动*/
容器内的内容水平居中align:center;

容器内的内容单行垂直居中的话,把行高line-height跟容器高设成一样就行了,
整个容器垂直居中的话该用用JS了
第3个回答  2010-04-09
为该div设置样式 margin:Xpx atuo;
//左右边距为atuo,它就会自动水平居中;X表示上下的外边距,上下相等,垂直方向自然会居中,不过要自己去调,和div的高度是有关系的
不知道是否另有高见者,等候佳音
第4个回答  2019-07-22
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.aaa{ width:300px; height:100px; background:#00CC66; line-height:100px; text-align:center;}
</style>
<title>景安网络</title>
</head>
<body>
<div class="aaa">景安网络</div>
</body>
</html>
我们可以使用text-align:center; line-height;属性设置器水平居中和垂直居中
相似回答