当前位置:网站首页>水平垂直居中 方法 和兼容
水平垂直居中 方法 和兼容
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
不要知道元素宽高:




缺点: 要知道元素宽高:




边栏推荐
- Xcode Revoke certificate
- 分类模型评价标准(performance measure)
- Cesium (4): the reason why gltf model is very dark after loading
- PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
- ThinkPHP URL 路由简介
- laravel怎么获取到public路径
- Prometheus API deletes all data of a specified job
- 面试题 01.02. 判定是否互为字符重排-辅助数组算法
- [C language] question set of X
- 【DesignMode】代理模式(proxy pattern)
猜你喜欢

Personal notes of graphics (1)

95. (cesium chapter) cesium dynamic monomer-3d building (building)

Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?

Leetcode-231-2的幂

Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()

Xcode Revoke certificate

AutoLISP series (1): function function 1

HAVE FUN | “飞船计划”活动最新进展

Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?

Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
随机推荐
Personal notes of graphics (1)
IP地址和物理地址有什么区别
[Android -- data storage] use SQLite to store data
【DesignMode】模板方法模式(Template method pattern)
js中复选框checkbox如何判定为被选中
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
Opportunity interview experience summary
Cesium (4): the reason why gltf model is very dark after loading
URL和URI的关系
Three singleton modes of unity (hungry man, lazy man, monobehavior)
Leetcode-231-2的幂
分步式监控平台zabbix
Logback logging framework third-party jar package is available for free
Odoo集成Plausible埋码监控平台
What else can an ordinary person do besides working in a factory to make money?
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
Logback日志框架第三方jar包 免费获取
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
Detailed explanation of several ideas for implementing timed tasks in PHP