【margin: auto为什么只能水平居中,不能垂直居中 _通用 】 | IT修真院·坑乎
margin: auto为什么只能水平居中,不能垂直居中
我也踩过这个坑( 2 )
已统计您的踩坑,无需重复点击
回答(1)
通用
详细描述
截图
代码
编辑于2024-04-16
  • [散修弟子]JS-2377
    1

    两个例子的区别

    HTML:

    <div id="centerTest1">

    <p>元素水平居中</p>

    </div>

    <div id="centerTest2">

    <p>元素水平垂直居中</p>

    </div>


    CSS:

    /*CSS 居中*/

    [id *= "centerTest"] {width: 150px;height: 150px;}

    #centerTest1 p {margin: auto;}

    #centerTest2 {position: relative;}

    #centerTest2 p {

    position: absolute;

    width: 100px;

    height: 20px;

    top: 0;

    bottom: 0;

    left: 0;

    right: 0;

    margin: auto;

    }



    编辑于2021-07-03