首页 > 其他 > 详细

元素水平垂直居中的几种常用方法

时间:2018-04-06 10:43:14      阅读:207      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>元素水平垂直居中</title>
        <style>
            /*方式一*/
            .box{
                width: 100px;
                height: 100px;
                background: red;
                position: absolute;
                top: 50%;
                left: 50%;
                margin-left: -50px;
                margin-top: -50px;
            }
            /*方式二*/
            .box{
                width: 100px;
                height: 100px;
                background: red;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50px,-50px);
                /*或者transform: translate(-50%,-50%);*/
            }
            /*方式三  技巧*/
            .box{
                width: 100px;
                height: 100px;
                background: red;
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                margin:auto;
            }
            /*方式四  弹性盒*/
            .father{
                display: flex;
                width: 400px;
                height: 400px;
                background: red;
                justify-content: center;
                align-items: center;
            }
            .son{
                width: 100px;
                height: 100px;
                background: aqua;
            }
        </style>
    </head>
    <body>
        <div class="box">我需要用多种方式居中你</div>
        <div class="father">
            <div class="son"></div>
        </div>
    </body>
</html>

 

元素水平垂直居中的几种常用方法

原文:https://www.cnblogs.com/lhl66/p/8724939.html

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