css
主页 > 网页 > css >

CSS3不定高宽垂直水平居中的实现几个方法

2020-03-26 | 秩名 | 点击:

1、flex布局

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

这种方式兼容性不好

2、position + transform

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

IE9以下不支持transform属性

3、table + table-cell

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

4、:before + display:inline-block

 
.father {
  text-align: center;
}
.father::before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
.son {
  display: inline-block;
}

原文链接:https://www.jb51.net/css/717624.html
相关文章
最新更新