现在的位置: 网页制作教程网站制作教程 >正文
CSS

css自适应屏幕垂直居中代码

发表于2016/11/26 网站制作教程 0条评论 ⁄ 热度 2,268℃

分享的这段css代码主要功能是可以让目标元素自适应屏幕(分辨率)垂直居中显示。

<!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>
<style type="text/css">
body {padding: 0; margin: 0;} 
body,html{height:100%;} 
#outer {height:100%; overflow: hidden; position:relative;width:100%; background:#333;} 
#outer[id] {display:table; position:static;} 
#middle {position: absolute; top:50%;left: 0} 
#middle[id] {display: table-cell; vertical-align: middle; position: static;} 
#content{width:500px; height:150px; border:1px solid red; background:#EEE; margin: 0 auto;} 
</style> 
</head>
<body> 
<div id="outer"> 
<div id="middle"> 
<div id="content">div层垂直居中</div> 
</div> 
</div> 
</body>
</html>

关于居中的文章还有:CSS控制元素在正中间

  • 暂无评论