纯css实现DIV以及图片水平垂直居中兼容多种浏览器(实现过程)

IE下

一、无法实现IE下垂直居中

可能出现的情况1、

<!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:500px; height:500px; border:3px solid #f00000; line-height:500px; vertical-align:middle; text-align:center;">
    <img src="http://www.jb51.net/images/logo.gif" style="vertical-align:middle; line-height:500px;" />
  </div>
</body>
</html>

可能出现的情况2、

<!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:500px; height:500px; border:3px solid #f00000; line-height:500px; vertical-align:middle; text-align:center;">
    <strong style="line-height:500px; vertical-align:middle;"> <img src="http://www.jb51.net/images/logo.gif" style="vertical-align:middle; line-height:500px; display:block;" /></strong>
  </div>
</body>
</html>

 

二、实现IE下垂直居中

<!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:500px; height:500px; border:3px solid #f00000; line-height:500px; vertical-align:middle; text-align:center;">
    <strong style="line-height:500px; vertical-align:middle;"> <img src="http://www.jb51.net/images/logo.gif" style="vertical-align:middle; line-height:500px;" /></strong>
  </div>
</body>
</html>

 

firefox、google等浏览器下

实现firefox、google等浏览器下垂直居中

<!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:500px; height:500px; border:3px solid #f00000; line-height:500px; vertical-align:middle; text-align:center; display:table-cell;">
    <strong style="line-height:500px; vertical-align:middle;"> <img src="http://www.jb51.net/images/logo.gif" style="vertical-align:middle; line-height:500px;" /></strong>
  </div>
</body>
</html>

个人总结:display:table-cell;要定义在一个具有具体宽高值的块元素容器里面,否则无法实现firefox等浏览器下垂直居中

 

 

posted @ 2013-11-06 11:48  志在,道在  阅读(603)  评论(0编辑  收藏  举报