首页 > 一个CSS垂直居中的问题

一个CSS垂直居中的问题

父元素的高度不知道

子元素是一个类型为 a 的元素

样式大概是这个样子

这样子的话要怎么居中呢?

谢谢大家的解答啦~


父元素不定高啊,line-hight 没法设,还有一个方法,父元素设置display: tabel-cell . vertical-align:middle. 也是没问题,,且兼容ie8+


提供两种方法参考
flex:

parent {
    display: flex;
    justify-content: center;
    align-items: center;
    align-content: center;
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;
}

transform:

a{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

line-height设置跟父层高度一样


position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:0 auto;

【热门文章】
【热门文章】