垂直居中的几种方式

来源:互联网 发布:淘宝宝贝标题范文 编辑:程序博客网 时间:2024/06/01 10:03

垂直居中的几种方式

1.ie7+以上支持

绝对定位+各外边距为0+margin:auto
<!doctype html><html lang="en"> <head>  <style>  .div1{  background:blue;  position:absolute;  left:0;  top:0;  right:0;  bottom:0;  height:100px;  width:100px;  margin:auto;  }  </style> </head> <body>  <div class = "div1">fsdaf</div> </body></html>

2. 支持ie6和其他浏览器(需要知道高度)

绝对定位+自身高度的一半
  <style>  div{  background:green;  height:100px;  width:120px;  position:absolute;  top:50%;  left:50%;  margin:-50px -60px;/*自身高度和宽度的一半*/  }  </style> </head> <body>  <div>fsdaf</div> </body>


3.自适应高度,支持ie6和其他浏览器

父级绝对定位+本身相对定位、inline-block、-50%;

</pre><pre name="code" class="html">  <style>  .infoBox{  position:absolute;  top:50%;  width:100%;  text-align:center;  }  .info{  display:inline-block;  position:relative;  top:-50%;  right:0;  border:1px solid;  }  </style> </head> <body>  <div class = "infoBox">    <div class = "info">     fsdafdfsg fdgfd</div>  </div> </body>



0 0
原创粉丝点击