首页 > Web开发 > 详细

CSS水平垂直居中

时间:2020-05-26 23:04:26      阅读:40      评论:0      收藏:0      [点我收藏+]
    <!--使一个div居中-->
        <div>
            1
            1
            1
        </div>
/*div宽高未知:通过绝对定位和transform*/
            div {
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                background-color: #AFEEEE;
            }
/*div宽高已知:通过绝对定位和margin*/
            div {
                position: absolute;
                top: 50%;
                left: 50%;
                margin: -100px;
                width: 200px;
                height: 200px;
                background-color: #AFEEEE;
            }
/*div宽高已知或未知:通过flex布局和margin*/
            body {
                display: flex;
                /*必须设置min-height为100vh*/
                min-height: 100vh;
                /*margin: 0;*/
            }
            div {
                margin: auto;
                /*此方法宽高已知未知都适用*/
                /*width: 200px;
                height: 200px;*/
                background-color: #AFEEEE;
            }
/*div宽高已知或未知:通过flex布局*/
            body {
                display: flex;
                /*必须设置min-height为100vh*/
                min-height: 100vh;
                justify-content: center;
                align-items: center;
            }
            div {
                /*此方法宽高已知未知都适用*/
                /*width: 200px;
                height: 200px;*/
                background-color: #AFEEEE;
            }

 

 

 

 

<!--使div中的p元素居中-->
        <div>
            <p>1</p>
        </div>
/*使用flex布局*/
            div {
                display:flex ;
                justify-content: center;
                align-items: center;
                width: 200px;
                height: 200px;
                background-color: #AFEEEE;
            }

 

/*使用table-cell和对齐*/
div { display: table-cell; text-align: center; vertical-align: middle; /*position: relative;
overflow:hidden;
*/ width: 200px; height: 200px; background-color: #AFEEEE; }
* {
   margin:0;
padding:0;
}

 

CSS水平垂直居中

原文:https://www.cnblogs.com/LangZ-/p/12968800.html

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