10种盒子垂直水平居中方案.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        
        /* 1.已知父子宽高,使用margin:0 auto;水平居中,再用margin移回自己高度的一半 ,
        父元素必须有边框*/
        .fu1{
            width: 200px;
            height: 200px;
            background-color: #ccc;
            border: 1px solid blue;
        }
        .son1{
            width: 100px;
            height: 100px;
            background-color: darkcyan;
            margin: 0 auto;
            margin-top: 50px;
        }
        /* 2.已知父子宽高,使用transform平移 */
        .fu2{
            width: 200px;
            height: 200px;
            background-color: #ccc;
            margin-top: 10px;
        }
        .son2{
            width: 100px;
            height: 100px;
            background-color:darkgreen;
            transform: translate(50px,50px);
        }
        /* 3.已知父子宽高,子绝父相,子元素通过绝对定位进行居中 */
        .fu3{
            width: 200px;
            height: 200px;
            background-color: #ccc;
            position: relative;
            margin-top: 10px;
        }
        .son3{
            width: 100px;
            height: 100px;
            background-color:darkmagenta;
            position: absolute;
            left: 50px;
            top: 50px;
        }
        /* 4.父元素只有宽度没有高度,子元素宽高已知,垂直方向通过padding设置,水平方向margin:0 auto */
        .fu4{
            width: 200px;
            background-color: #ccc;
            margin-top: 10px;
            padding:50px 0;
        }
        .son4{
            width: 100px;
            height: 100px;
            background-color:darksalmon;
            margin: 0 auto; 
        }
        /* 5.父级元素必须有边框,通过给子元素设置相对定位偏移父级的50%之后,
        使用margin移回自己的一半宽高 */
        .fu5{
            width: 200px;
            height: 200px;
            background-color: #ccc;
            border: 1px solid red;
            margin-top: 10px;
        }
        .son5{
            width: 100px;
            height: 100px;
            background-color:rgb(80, 36, 121);
            position: relative;
            top: 50%;
            left: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }
        /* 6.通过给子元素设置相对定位偏移父级的50%之后,通过transform移回自己的50% */
        .fu6{
            width: 200px;
            height: 200px;
            background-color: #ccc;
            /* border: 1px solid red; */
            margin-top: 10px;
        }
        .son6{
            width: 100px;
            height: 100px;
            background-color:darkgoldenrod;
            position: relative;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
        }
        .fu7{
            width: 200px;
            height: 200px;
            background-color: #ccc; 
            margin-top: 10px;
        }
        /* 7.通过给子元素前面添加一个高度为父级元素一半的子元素,再
        使用margin移回高度的一半,水平方向使用margin:0 auto居中。 */
        .son71{
            /* width: 100%; */
            height: 50%;
        }
        .son7{
            width: 100px;
            height: 100px;
            background-color:indigo;
            margin:0 auto;
            margin-top: -50px;
        }
         /* 8.通过将父元素设置为弹性盒子,使用justify-content水平居中,
        align-items垂直居中,子元素有无宽高均可 */
        .outter1{
            width: 200px;
            height: 200px;
            background-color: #ccc;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-bottom: 10px;
        }
        /* .inner1{
            width: 100px;
            height: 100px;
            background-color:lawngreen;
        } */

        /* 9.使用display:table-cell让元素以表格单元格的形式呈现,
        使用vertical-align实现垂直居中 ,再通过给子元素设置margin:0 auto;实现水平居中*/
        .outter2{
            
            width: 200px;
            height: 200px;
            background-color: #ccc;
            display: table-cell;
            vertical-align: middle;
            /* margin: 0 auto;
            border:1px solid transparent; */
        }
        .inner2{
            width: 100px;
            height: 100px;
            background-color:lightcoral;
            margin: 0 auto;
        }
        /* 10. flex布局+子元素设置margin:auto; */
        .outter3{
            margin-top: 10px;
            width: 200px;
            height: 200px;
            background-color: #ccc;
            display: flex;
        }
        .inner3{
            width: 100px;
            height: 100px;
            background-color: turquoise;
            margin: auto;
        }
    </style>
</head>
<body>
    <!--  1.-->
    <div class="fu1">
        <div class="son1"></div>
    </div>
    
    <div class="fu2">
        <div class="son2"></div>
    </div>
    <div class="fu3">
        <div class="son3"></div>
    </div>
    <div class="fu4">
        <div class="son4"></div>
    </div>
    <div class="fu5">
        <div class="son5"></div>
    </div>
    <div class="fu6">
        <div class="son6"></div>
    </div>
    <div class="fu7">
        <div class="son71"></div>
        <div class="son7"></div>
        
    </div>
    <div class="outter1">
        <!-- <div class="inner1"></div> -->
        <span>我是span</span>
    </div>
    <div class="outter2">
        <div class="inner2"></div>
    </div>
    <div class="outter3">
        <div class="inner3"></div>
    </div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值