当前位置:网站首页>水平垂直居中 方法 和兼容
水平垂直居中 方法 和兼容
2022-07-07 14:19:00 【星空小梦】
方法
第一种通过绝对定位的方式 absolute + 负margin
首先知道子元素的宽高,给子元素设置top:50%;left:50%,
但绝对定位是基于子元素的左上角,我们所希望的效果是子元素的中心居中显示。。。。借助外边距的负值,负的外边距可以让元素向相反方向定位,
通过指定子元素的外边距为子元素宽度一半的负值,就可以让子元素居中了
**优点:**比较好理解,兼容性好
**缺点:**需要知道子元素的宽高
第二种:也是通过绝对定位的方式 absolute + margin auto
这个是需要将各个方向的距离都设0,再讲margin设为auto;就行
**优点:**兼容性也很好
**缺点:**需要知道子元素的宽高
第三种:absolute + calc(计算)
这种方法top的百分比是基于元素的左上角,那么在减去宽度与高度的一半就好了
calc:任何长度值都可以使用calc()函数进行计算;
calc()函数使用标准的数学运算优先级规则;
它支持 “+”, “-”, “*”, “/” 运算
也可以查看calc教程:https://www.runoob.com/cssref/func-calc.html
**优点:**他的兼容性依赖的是calc的兼容性
**缺点:**同样是需要知道子元素的宽高
第四种:absolute + transform (过渡)
这个方法不需要子元素固定宽高
修复绝对定位的问题,还可以使用css3新增的transform,transform的translate
属性也可以设置百分比,其是相对于自身的宽和高,所以可以将translate设置为-50%,就可以做到居中了
**优点:**代码量少
**缺点:**IE8不支持, 属性需要追加浏览器厂商前缀, 可能干扰其他 transform 效果, 某些情形下会出现文本或元素边界渲染模糊的现象.
第五种:line-height

优点:代码简洁
缺点:只对文本有效,只对单行文本有效,多行文本不可以
第九种:弹性盒子的方式
通过给父元素设置justify-content: center;
align-items: center;就可以了
**优点:**移动端使用灵活自如
**缺点:**pc端需要根据兼容情况来判定
第十种:grid(网格布局)
给父级设display:grid;
给子元素设align-self: center;justify-self: center;
**优点:**代码量少
**缺点:**兼容不如flex,建议用flex
不要知道元素宽高:
缺点: 要知道元素宽高:
边栏推荐
- JS中null NaN undefined这三个值有什么区别
- 【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
- Geoserver2.18 series (5): connect to SQLSERVER database
- 记录Servlet学习时的一次乱码
- Imitate the choice of enterprise wechat conference room
- 【DesignMode】模板方法模式(Template method pattern)
- "The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
- three.js打造酷炫下雪效果
- Step by step monitoring platform ZABBIX
- Three. JS series (2): API structure diagram-2
猜你喜欢
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
【Android -- 数据存储】使用 SQLite 存储数据
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
分步式监控平台zabbix
【C 语言】 题集 of Ⅹ
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
如何快速检查钢网开口面积比是否符合 IPC7525
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
随机推荐
Prediction - Grey Prediction
全网“追杀”钟薛高
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
Performance measure of classification model
pycharm 终端部启用虚拟环境
Balanced binary tree (AVL)
logback.xml配置不同级别日志,设置彩色输出
Xcode Revoke certificate
HAVE FUN | “飞船计划”活动最新进展
Notification uses full resolution
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
【C 语言】 题集 of Ⅹ
Talk about the cloud deployment of local projects created by SAP IRPA studio
What else can an ordinary person do besides working in a factory to make money?
121. The best time to buy and sell stocks
Opportunity interview experience summary
1亿单身男女“在线相亲”,撑起130亿IPO
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
PHP has its own filtering and escape functions
[summary of knowledge] summary of notes on using SVN in PHP