首页 > 其他 > 详细

DIV水平垂直居中

时间:2020-06-15 16:26:49      阅读:60      评论:0      收藏:0      [点我收藏+]

 

<div class="father">
        <div class="son"></div>
</div>

方案一:通过父相子绝 自走父的50%再回走自身的一半

   .father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            position: relative;
        }
        
    .son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }

方案二:通过父相子绝  子 margin: auto; top: 0;left: 0; right: 0;bottom: 0;

.father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            position: relative;
        }
        
.son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
            position: absolute;
            margin: auto;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }

方案三:父相子绝 利用父的text-align实现子的居中 子需要转行内块 并回走自身一半

        .father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            position: relative;
            text-align: center;
           
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
            display: inline-block;
            position: absolute;
            top: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }

方案四:父相子绝 子走父50% 再过渡回自己的一半

        .father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            position: relative;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

方案五:flex伸缩布局 主轴 副轴 都居中

        .father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
        }

 

DIV水平垂直居中

原文:https://www.cnblogs.com/fortuneteller-li/p/13131374.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!