您现在的位置:首页 >> 前端 >> 内容

不定宽高水平且垂直居中的实现方法

时间:2017/8/5 14:51:31 点击:

  核心提示:不定宽高水平且垂直居中的实现方法flex布局效果预览水平垂直居中p.parent {display: flex;justify-content: center; /*主轴对齐方式*/align-ite...

不定宽高水平且垂直居中的实现方法

flex布局

效果预览


水平垂直居中

p.parent {
  display: flex;
  justify-content: center; /*主轴对齐方式*/
  align-items: center; /*交叉轴上如何对齐*/
  background: #EDEDED;
  width: 800px;
  height: 400px;
}
p.child {
  background: green;
}

绝对定位+transform

预览地址


水平垂直居中

p.parent {
  position: relative;
  width: 800px;
  height: 300px;
  background: #EDEDED;
}
p.child {
  background: green;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

table-cell

效果预览


水平垂直居中

p.parent {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  background: #EDEDED;
  width: 300px;
  height: 300px;
}
p.child {
  vertical-align: center;
  display: inline-block;
  background: green;
}

作者:网络 来源:Dear_Mr的博客
  • 上一篇:image的srcset属性
  • 下一篇:HTML与xml的区别