18910140161

几个常用的水平 垂直和中心对齐方法

顺晟科技

2021-08-31 12:18:25

152

CSS--几种常用的水平垂直居中对齐方法

目录
  • CSS--几种常用的水平垂直居中对齐方法
    • 文字的水平垂直居中
    • 元素的水平垂直居中

文字的水平垂直居中

    text-align: center;
    line-height: 单前元素高度;

元素的水平垂直居中

1 使用定位

   /* 需要固定宽高 */
    position: absolute;
    top:0;
    left: 0;
    bottom: 0;
    right: 0;
    width: xxx;
    height: xxx
    margin: auto;

2 使用定位+ calc()

    /* 需要知道具体的元素宽高值 */
    position: absolute;
    width: xxx;
    height: xxx;
    top: calc(50% - xxx/2);
    left: calc(50% - xxx/2)

3. 使用定位+transform

   /* 不需要知道元素的宽高 */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

4. 使用display:flex;

    display: flex;
    justify-content: center;
    align-items: center;
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航