水平垂直居中

水平垂直居中是最常用到的布局,那么常用的解决方案都有哪些呢,下面就依次来列举一下

例子:

HTML代码:

<div class="container">
    <div class="inner">内容</div>
</div>

常用解决方案类别:

一.脱离文档流方式的居中

  1. 负margin的方式(inner宽高知道)

.container{
    position: relative;
}
.inner{
    width:100px;
    height:30px;
    position: absolute;
    top:50%;
    left:50%;
    margin-top:-15px;
    margin-left:-50px;
}

2.transform:translate的方式

3.margin:auto的方式

4.css3的 calc属性方式(固定高度

二.未脱离文档流方式的居中

1.line-height方式实现(固定高度)

2.flex布局

3.table方式

Last updated

Was this helpful?