当前位置:网站首页>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 :
边栏推荐
- Prediction - Grey Prediction
- 23. 合并K个升序链表-c语言
- 【Vulnhub靶场】THALES:1
- 【知识小结】PHP使用svn笔记总结
- prometheus api删除某个指定job的所有数据
- ThinkPHP URL 路由简介
- [C language] question set of X
- PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
- php 自带过滤和转义函数
- Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
猜你喜欢
What about the pointer in neural network C language
Balanced binary tree (AVL)
The difference and working principle between compiler and interpreter
AutoLISP series (3): function function 3
【Vulnhub靶场】THALES:1
企业级日志分析系统ELK
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
Personal notes of graphics (3)
随机推荐
Laravel service provider instance tutorial - create a service provider test instance
【C 语言】 题集 of Ⅹ
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
What is the difference between IP address and physical address
You Yuxi, coming!
Three. JS series (3): porting shaders in shadertoy
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
three.js打造酷炫下雪效果
URL和URI的关系
Unity3d click events added to 3D objects in the scene
Talk about the cloud deployment of local projects created by SAP IRPA studio
Common training data set formats for target tracking
Pycharm terminal enables virtual environment
Logback logging framework third-party jar package is available for free
Xcode Revoke certificate
The difference and working principle between compiler and interpreter
Usage of config in laravel
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
Cesium (4): the reason why gltf model is very dark after loading
prometheus api删除某个指定job的所有数据