首页 > Web开发 > 详细

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

时间:2020-03-20 16:29:16      阅读:60      评论:0      收藏:0      [点我收藏+]

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;

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

原文:https://www.cnblogs.com/luoshuifushen/p/12532157.html

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