博客
关于我
前端笔记(总结几种水平垂直居中的方式)
阅读量:462 次
发布时间:2019-03-06

本文共 952 字,大约阅读时间需要 3 分钟。

基本是网上看来的,前2种用的较多,第三种是有点黑科技的写法

如果只是水平居中,就可以直接使用margin:0 auto;

 

第一种:使用translate

属于transform的一个方法,将div进行百分比xy轴位移

.d1_1{    width: 200px;    height: 200px;    background-color: red;}.d2_1{    width: 100px;    height: 100px;    background: blue;    transform: translate(50%,50%);/*位移*/}

当然,最好使用相对定位加绝对定位先,为了不影响其他元素排列

 

第二种:使用弹性布局flex

大多数用这种方式,设置容器的属性即可,不过要注意浏览器兼容

.d3{    width: 200px;    height: 200px;    background-color: red;    display: flex;    align-items: center;/*垂直居中*/    justify-content: center;/*水平居中*/}.d4{    width: 100px;    height: 100px;    background: blue;}

 

第三种:这种是黑科技,使用vertical-align

不建议多用也比较麻烦,用到了水平居中、变为行内元素,居中

.d5{    width: 200px;    height: 200px;    background-color: red;    text-align: center;}.d5::after{    content: '';    display: inline-block;    height: 100%;    vertical-align: middle;}.d6{    width: 100px;    height: 100px;    background-color: blue;    display: inline-block;    vertical-align: middle;}

 

转载地址:http://rmbbz.baihongyu.com/

你可能感兴趣的文章
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>