块元素居中

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            .box{
                width: 300px;
                height: 300px;
                border: 2px solid red;
                margin: 20px auto 0;
                position: relative;
            }
            .div1{
                width: 100px;
                height: 100px;
                background-color: blue;
                /*居中*/
                position: absolute;
                left: 0;
                right: 0;
                top: 0;
                bottom: 0;
                margin: auto;
            }
            span{
                width: 100px;
                height: 100px;
                position: absolute;
                left: 0;
                right: 0;
                top: 0;
                bottom: 0;
                margin: auto;
                
            }
        </style>
    </head>
    <body>
        <!--
            块元素居中:
            1.水平:块元素宽度固定,左右margin值为auto
            2.竖直:将其宽高固定,绝对定位,上下margin给auto
            
            最快速的水平垂直都居中:将其宽高固定,绝对定位,所有方向margin给auto
        -->
        
        <div class="box">
            <!--<div class="div1"></div>-->
            <span>与三九年间</span>
        </div>
        
    </body>
</html>

 

posted @ 2021-01-21 17:37  明坤  阅读(169)  评论(0)    收藏  举报