# css控制div垂直且水平的方法(不常用但是却很有效的)

忍无可忍,无需再忍,自己曾经走过的坑,给后来小伙伴填上哦,那些div居中的方法总结。

- 1.父级元素display:table-cell;vertercal-align:middle;子级margin:0px auto;//上代码!(注意只兼容ie8和ie8以上浏览器)

html部分 :<div id="content">
           <div class="center">你好</div>
        </div>
css部分:#content{
                 width:800px;height:500px;
                display:table-cell;vertical-align: middle;
                border:1px solid #f80;
                }
     #content .center{
                  border:1px solid #f80;width:100px;
                  height:100px;margin:0px auto;
      }

  • 2.利用绝对定位position。以及top,left,right,bottom,margin:auto;zoom:1;等属性//直接上代码!(兼容ie8和ie8以上浏览器!)
html部分 :<div id="content">
           <div class="center">你好</div>
        </div>
css部分:#content{
                 width:800px;height:500px;position:relative;
                border:1px solid #f80;
                }
     #content .center{
                  position:absolute;top:0px;left:0px;right:0px;
                  bottom:0px;margin:auto;zoom:1;//zoom是用来兼容ie8以下的
                  border:1px solid #f80;width:100px;
                  height:100px;
      }

3.利用css3 transform属性,绝对定位,以及定位top,left属性!(兼容ie8以上)

html部分 :<div id="content">
           <div class="center">你好</div>
        </div>
css部分:#content{
                 width:800px;height:500px;position:relative;
                border:1px solid #f80;
                }
     #content .center{
                  position:absolute;top:50%;left:50%;
                  transform:translate(-50%,-50%);
                  border:1px solid #f80;width:100px;
                  height:100px;
      }

我走过的坑,不希望后来小伙伴再走!路过小伙伴觉得文章有用不吝啬给攒啊!或者关注一波!你懂得!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值