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




缺点: 要知道元素宽高:




边栏推荐
- laravel怎么获取到public路径
- Prometheus API deletes all data of a specified job
- Laravel 中config的用法
- 目标跟踪常见训练数据集格式
- Communication mode between application program and MATLAB
- Common training data set formats for target tracking
- laravel post提交数据时显示异常
- 2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
- Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
- Continuous creation depends on it!
猜你喜欢

记录Servlet学习时的一次乱码

Three. JS series (1): API structure diagram-1

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
![[Android -- data storage] use SQLite to store data](/img/f6/a4930276b3da25aad3ab1ae6f1cf49.png)
[Android -- data storage] use SQLite to store data

Plate - forme de surveillance par étapes zabbix

如何快速检查钢网开口面积比是否符合 IPC7525

【Vulnhub靶场】THALES:1

【Android -- 数据存储】使用 SQLite 存储数据

The difference and working principle between compiler and interpreter

Cesium(3):ThirdParty/zip. js
随机推荐
Xcode Revoke certificate
Logback logging framework third-party jar package is available for free
Performance measure of classification model
three. JS create cool snow effect
95. (cesium chapter) cesium dynamic monomer-3d building (building)
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
47_ Contour lookup in opencv cv:: findcontours()
Personal notes of graphics (2)
分步式監控平臺zabbix
laravel怎么获取到public路径
Three singleton modes of unity (hungry man, lazy man, monobehavior)
torch. Numel action
What about the pointer in neural network C language
3000 words speak through HTTP cache
【DesignMode】外观模式 (facade patterns)
【C 语言】 题集 of Ⅹ
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
修改配置文件后tidb无法启动
AutoLISP series (3): function function 3
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?