代码改变世界

父元素高度确的多行文本、图片、块级元素的竖直居中

2012-12-10 09:10  greenal  阅读(311)  评论(0编辑  收藏  举报
《编写高质量代码》 的笔记
 
<!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=utf-8" />
<title>父元素高度确的多行文本、图片、块级元素的竖直居中</title>

<style>
/*支持table-cell的IE8和火狐设置,不支持的IE6,7设置hack,通过给父子两层元素分别设置 tOP:50%; 和 top:-50%; 来实现居中。*/
.mb10{ margin-bottom:10px;}
.wrap{ background:3#093; width:500px; height:100px; color:#fff; margin-bottom:10px; display :table-cell;vertical-align:middle; *position:relative;}

.test{ width:200px; height:50px; background:#F66;}
.verticalWrap{*position:absolute; *top:50%;}
.vertical{*position:relative; *top:-50%;}


</style>
</head>

<body>
<div class="mb10">
  <div class="wrap">
    <div class="verticalWrap">
      <div class="vertical"> hello guoguo<br />
        hello guoguo<br />
        hello guoguo </div>
    </div>
  </div>
</div>
<br />
<div class="mb10">
  <div class="wrap">
    <div class="verticalWrap">
      <img class="vertical" src="images/1.jpg" width="200" height="200" />
    </div>
  </div>
</div>
<br />
<div class="mb10">
  <div class="wrap">
    <div class="verticalWrap">
      <div class="test vertical">
      </div>
    </div>
  </div>
</div>
<br />
</body>
</html>