垂直居中的几种方式

垂直居中的几种方式

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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值