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




缺点: 要知道元素宽高:




边栏推荐
- Set the route and optimize the URL in thinkphp3.2.3
- Markdown formula editing tutorial
- [summary of knowledge] summary of notes on using SVN in PHP
- MySQL数据库基本操作-DQL-基本查询
- [vulnhub range] thales:1
- ThinkPHP URL 路由简介
- Tidb cannot start after modifying the configuration file
- 统计学习方法——感知机
- You Yuxi, coming!
- "The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
猜你喜欢

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"?

AutoLISP series (1): function function 1

Sysom case analysis: where is the missing memory| Dragon lizard Technology

torch.numel作用

【DesignMode】代理模式(proxy pattern)

1亿单身男女“在线相亲”,撑起130亿IPO

平衡二叉树(AVL)

【DesignMode】外观模式 (facade patterns)

spark调优(三):持久化减少二次查询

Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
随机推荐
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
JS中null NaN undefined这三个值有什么区别
Three. JS series (2): API structure diagram-2
thinkphp3.2.3中设置路由,优化url
URL和URI的关系
目标跟踪常见训练数据集格式
Unity3d click events added to 3D objects in the scene
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
Usage of config in laravel
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
Talk about the cloud deployment of local projects created by SAP IRPA studio
AutoLISP series (3): function function 3
Detailed explanation of several ideas for implementing timed tasks in PHP
面试题 01.02. 判定是否互为字符重排-辅助数组算法
Common training data set formats for target tracking
删除 console 语句引发的惨案
logback. XML configure logs of different levels and set color output