div代码居中 上下居中 左右居中
2013-10-31 10:16
357 查看
<!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>Universal vertical center with css</title>
<style>
#outer {
height:300px;
width:300px;
overflow:hidden;
position:relative;
text-align:center;
}
#outer[id]{
display:table;
position:static;
}
#middle{
position:absolute;
top:50%;
}
/* for explorer only*/
#middle[id]{
display:table-cell;
vertical-align:middle;
position:static;
}
#inner{
position:relative;
top:50%;
}
/* for explorer only*/
/* optional : #inner[id] {position:static}*/
.withBorder{
border:1px green solid;
}
</style>
</head>
<body>
<div id="outer" class="withBorder">
<div id="middle">
<div id="inner">
<img src="http://blog.163.com/lixiaofengfly@126/blog/image/guest_test_icon2.jpg"/>
<!-- any text any height any content,
everthing is vertically centered.
或者文字也可以
只需要设置 outer 的 高度 宽度 就可以得到想要的效果了
其他... 复制!
-->
</div>
</div>
</div>
</body>
</html>
精简后的代码
<!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>Universal vertical center with css</title>
<style>
#outer {
height:300px;
width:300px;
display:table-cell;
vertical-align:middle;/*火狐,chrom垂直居中*/
line-height:300px; /*IE7垂直居中*/
text-align:center; /*水平居中*/
}
.withBorder{
border:1px green solid;
}
</style>
</head>
<body>
<div id="outer" class="withBorder">
<img src="http://blog.163.com/lixiaofengfly@126/blog/image/guest_test_icon2.jpg"/>
</div>
</body>
</html>
相关文章推荐
- DIV 上下左右居中
- 如何让图片在div里左右居中,上下居中
- CSS3实现图片在DIV中上下左右居中(2)
- CSS实现图片在DIV中上下左右居中(1)
- 一个div层在页面上下左右居中以及数据的排序
- div完美自适应动态上下左右居中 (转)
- div的上下居中,左右居中,左右上下都居中
- div内容上下左右居中
- DIV上下左右都居中--经测试兼容IE,FF,Opera,Google Chrome
- 一个上下左右都居中的非table的纯CSS实现代码
- 两种方式让DIV上下左右都居中--经测试兼容IE,FF,Opera,Google Chrome
- div中的文字上下左右居中的方法
- div+css:页面整体布局居中显示:上下居中||垂直居中,左右居中||水平居中
- 水平垂直居中的四种方法 div在div中上下左右居中
- div完美自适应动态上下左右居中
- 纯CSS实现上下左右都居中的代码
- css div上下左右居中
- div元素上下左右居中
- 7.使用相对定位进行div的上下左右居中
- css:子元素div 上下左右居中方法总结