博客
关于我
前端笔记(总结几种水平垂直居中的方式)
阅读量: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执行顺序与索引算法
查看>>
mysql批量update优化_Mysql中,21个写SQL的好习惯,你值得拥有呀
查看>>
mysql批量update操作时出现锁表
查看>>
MYSQL批量UPDATE的两种方式
查看>>
mysql批量修改字段名(列名)
查看>>
MySQL批量插入数据遇到错误1213的解决方法
查看>>
mysql技能梳理
查看>>
MySQL报Got an error reading communication packets错
查看>>
Mysql报错Can‘t create/write to file ‘/tmp/#sql_3a8_0.MYD‘ (Errcode: 28 - No space left on device)
查看>>
MySql报错Deadlock found when trying to get lock; try restarting transaction 的问题解决
查看>>
MySQL报错ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘
查看>>
Mysql报错Packet for query is too large问题解决
查看>>
mysql报错级别_更改MySQL日志错误级别记录非法登陆(Access denied)
查看>>
Mysql报错:too many connections
查看>>
MySQL报错:无法启动MySQL服务
查看>>
mysql授权用户,创建用户名密码,授权单个数据库,授权多个数据库
查看>>
mysql排序查询
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
Mysql推荐书籍
查看>>