css水平垂直居中的几种方式
2018-08-17 11:35
771 查看
html代码如下:
//html <div class="parent"> <div class="child"></div> </div>
一 、脱离文档流的居中
1、margin: auto居中
.parent{ width: 900px; height: 400px; background: red; position: relative; } .child{ width: 50%; height: 50%; background: blue; position: absolute; margin: auto; left: 0; top: 0; right: 0; bottom: 0; }
2、负margin居中
.parent{ width: 900px; height: 400px; background: red; position: relative; } .child{ width: 400px; height: 300px; background: blue; position: absolute; left: 50%; top: 50%; margin-left: -200px; margin-top: -150px; }
二、未脱离文档流的水平垂直居中
1、display:table-cell居中
.parent{ width: 900px; height: 400px; background: red; display: table-cell; vertical-align: middle; } .child{ width: 50%; height: 50%; background: blue; margin: 0 auto; }
2、flex居中
.parent{ width: 900px; height: 400px; background: red; display: flex; display: -webkit-flex; align-items: center; //控制垂直方向居中 -webkit-align-items: center; justify-content: center; //控制水平方向居中 -webkit-justify-content: center; } .child{ width: 50%; height: 50%; background: blue; }
ok~ 本篇圆满完结~
阅读更多相关文章推荐
- css让容器水平垂直居中的几种方式总结
- css 水平垂直居中的几种常见方式
- CSS实现水平垂直居中的几种方式
- 七种CSS方式让一个容器水平垂直居中
- CSS水平垂直居中的几种方法
- 水平垂直居中的几种方式
- css几种方法使元素水平垂直居中
- css 几种水平垂直居中的方法 及 弹性盒子
- 七种CSS方式让一个容器水平垂直居中
- [分享] 纯CSS完美实现垂直水平居中的6种方式
- 几种常见的水平垂直居中方式
- CSS垂直水平居中方式大全(一)----水平居中-垂直居中
- CSS水平垂直居中的几种方法
- CSS实现水平垂直居中的几种方法
- HTML中CSS文本、子div、图片水平居中和垂直居中的几种方法
- css垂直居中的几种方式
- 【css】实现垂直水平居中的几种方法
- CSS实现垂直、水平居中方式
- 七种CSS方式让一个容器水平垂直居中
- 最近一个刚刚毕业的朋友说,他面试时候,遇到最频繁的css问题就是垂直居中,这里给出几种垂直居中方式!