不定高寬垂直水平居中的幾種方式

一、flex佈局

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

這種方式兼容性很差佈局

二、position + transform

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

IE9如下不支持transform屬性flex

三、table + table-cell

.father {
    display: table;
}
.son {
    display: table-cell;
    vertical-align: middle;
      text-align: center;
}

四、:before + display:inline-block

.father {
  text-align: center;
}
.father::before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
.son {
  display: inline-block;
}
相關文章
相關標籤/搜索