6种方法将一个div水平垂直居中

方案一:

div绝对定位水平垂直居中【margin:auto实现绝对定位元素的居中】,

兼容性:IE7及之前版本不支持

    div{
            width: 200px;
            height: 200px;
            background: green;
            position:absolute;
            left:0;
            top: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

在这里插入图片描述

方案二:

div绝对定位水平垂直居中【margin 负间距】 这或许是当前最流行的使用方法。

         div{
            width:200px;
            height: 200px;
            background:green;
            position: absolute;
            left:50%;
            top:50%;
            margin-left:-100px;
            margin-top:-100px;
        }        

方案三:

div绝对定位水平垂直居中【Transforms 变形】

兼容性:IE8不支持;

        div{
            width: 200px;
            height: 200px;
            background: green;
            position:absolute;
            left:50%;    /* 定位父级的50% */
            top:50%;
            transform: translate(-50%,-50%); /*自己的50% */
        }            

方案四:
css不定宽高水平垂直居中

    .box{
             height:600px;
             display:flex;
             justify-content:center;
             align-items:center;
               /* aa只要三句话就可以实现不定宽高水平垂直居中。 */
        }
        .box>div{
            background: green;
            width: 200px;
            height: 200px;
        }

方案五:

将父盒子设置为table-cell元素,可以使用text-align:center和vertical-align:middle实现水平、垂直居中。比较完美的解决方案是利用三层结构模拟父子结构。

  <p class="outerBox tableCell">
  	<p class="ok"></p>
  	<p class="innerBox">tableCell</p>
  </p>
  <p></p>
  <p></p>
/*
table-cell实现居中
将父盒子设置为table-cell元素,设置
text-align:center,vertical-align: middle;
子盒子设置为inline-block元素
*/
.tableCell{
  display: table;
}
.tableCell .ok{
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}
.tableCell .innerBox{
  display: inline-block;
}

方案六:
对子盒子实现绝对定位,利用calc计算位置

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML5Course - HTML5学堂 - 利用CSS3的calc( )实现水平垂直居中</title>
    <link rel="stylesheet" href="reset.css">
    <style>
        .wrap {
            width: 400px;
            height: 400px;
            background-color: #ccc;
        }
        .wrap .con {
            /*利用CSS3的calc() ,它的用法类似于函数,能够给元素设置动态的值:*/
            /*(父级的宽度 - 自身的宽度)的一半当做padding的值*/
            padding: -webkit-calc((100% - 100px) / 2);
            padding: -moz-calc((100% - 100px) / 2);
            padding: -ms-calc((100% - 100px) / 2);
            padding: calc((100% - 100px) / 2);            
            width: 100px;
            height: 100px;
            background-color: pink;
            color: #fff;
            /*背景只显示内容区域*/
            background-clip: content-box;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="con">HTML5学堂 - 刘国利、陈能堡</div>
    </div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值