当前位置:网站首页>Horizontal and vertical centering method and compatibility
Horizontal and vertical centering method and compatibility
2022-07-07 16:33:00 【Starry dream】
Method
The first way is through absolute positioning absolute + negative margin
First of all, we know the width and height of sub elements , Set... For child elements top:50%;left:50%,
But the absolute positioning is based on the upper left corner of the child element , The effect we want is to center the child elements .... With the help of the negative value of the outer margin , Negative outer margins allow elements to be positioned in the opposite direction ,
By specifying that the outer margin of the child element is a negative value that is half the width of the child element , You can center the child elements
** advantage :** It's easy to understand , Compatibility is good.
** shortcoming :** You need to know the width and height of the child elements
The second kind : Also through the way of absolute positioning absolute + margin auto
This is to set the distance in all directions 0, Let's talk about it again margin Set to auto; Just go
** advantage :** Compatibility is also good
** shortcoming :** You need to know the width and height of the child elements
The third kind of :absolute + calc( Calculation )
This method top The percentage of is based on the upper left corner of the element , Then subtract half of the width and height
calc: Any length value can be used calc() Function to calculate ;
calc() Function uses standard mathematical operation priority rules ;
It supports “+”, “-”, “*”, “/” operation
You can also view calc course :https://www.runoob.com/cssref/func-calc.html
** advantage :** His compatibility depends on calc The compatibility of
** shortcoming :** It is also necessary to know the width and height of child elements
A fourth :absolute + transform ( transition )
This method does not require the fixed width and height of child elements
Fix the absolute positioning problem , You can also use css3 Newly added transform,transform Of translate
Property can also set the percentage , It's relative to its width and height , So you can translate Set to -50%, You can center it
** advantage :** Less code
** shortcoming :**IE8 I won't support it , Property needs to add browser manufacturer prefix , May interfere with other transform effect , In some cases, text or element boundaries are rendered blurred .
The fifth :line-height

advantage : The code is concise
shortcoming : Valid only for text , Only valid for single line text , Multiline text cannot
The ninth : Elastic box way
By setting... To the parent element justify-content: center;
align-items: center; That's all right.
** advantage :** The mobile terminal can be used flexibly
** shortcoming :**pc The end needs to be judged according to the compatibility
The tenth :grid( Grid layout )
Set for parent display:grid;
Set the child element to align-self: center;justify-self: center;
** advantage :** Less code
** shortcoming :** Compatibility is not as good as flex, Suggest using flex
Don't know the width and height of the element :




shortcoming : Know the width and height of the element :




边栏推荐
- 预测——灰色预测
- Unity3d click events added to 3D objects in the scene
- 23. 合并K个升序链表-c语言
- Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
- PHP实现执行定时任务的几种思路详解
- Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
- The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
- 华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
- Communication mode between application program and MATLAB
- Laravel constructor and middleware execution order
猜你喜欢

Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition

Continuous creation depends on it!

SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

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

Personal notes of graphics (1)

2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo

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

MySQL数据库基本操作-DQL-基本查询

Step by step monitoring platform ZABBIX

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
随机推荐
laravel怎么获取到public路径
ThinkPHP URL 路由简介
Opportunity interview experience summary
Logback日志框架第三方jar包 免费获取
Laravel5.1 Routing - routing packets
Notification uses full resolution
记一次项目的迁移过程
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
Xcode Revoke certificate
JS中null NaN undefined这三个值有什么区别
【DesignMode】外观模式 (facade patterns)
Geoserver2.18 series (5): connect to SQLSERVER database
Plate - forme de surveillance par étapes zabbix
Statistical learning method -- perceptron
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
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"?
Tragedy caused by deleting the console statement
Leetcode-136-只出现一次的数(用异或来解答)
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)