元素水平垂直居中的方式有哪些?

和自甴很熟 提交于 2019-12-24 12:02:17

1、absolute加margin方案

div{
        position: absolute;
        width: 100px;
        height: 100px;
        left: 50%;
        top: 50%:
        margin-top: -50px;
        margin-left: -50px;
    }

2、fixed 加 margin 方案

div{
        position: fixed;
        width: 100px;
        height: 100px;
        top: 0;
        right:0;
        left: 0;
        bottom: 0;
        margin: auto;
    }

3、display:table 方案

div{
        display: table-cell;
        vertical-align: middle;
        text-align: center;
        width: 100px;
        height: 100px;
    }

4、行内元素line-height方案

div{
        text-align: center;
        line-height: 100px;
    }

5、flex 弹性布局方案

div{
        display: flex;
        align-items: center;
        justify-content:center
    }

6、transform 未知元素宽高解决方案

div{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%)
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!