当前位置:网站首页>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 :
边栏推荐
- laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
- asyncio 概念和用法
- [flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
- 【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
- Leetcode-136- number that appears only once (solve with XOR)
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
- Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
- laravel post提交数据时显示异常
- Logback logging framework third-party jar package is available for free
- PHP实现执行定时任务的几种思路详解
猜你喜欢
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
Step by step monitoring platform ZABBIX
Logback logging framework third-party jar package is available for free
What about the pointer in neural network C language
Vs tool word highlight with margin
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
【DesignMode】外观模式 (facade patterns)
Mysql database basic operation DQL basic query
Opencv configuration 2019vs
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
随机推荐
Spark Tuning (III): persistence reduces secondary queries
AutoLISP series (2): function function 2
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
【Android -- 数据存储】使用 SQLite 存储数据
模仿企业微信会议室选择
Talk about the cloud deployment of local projects created by SAP IRPA studio
Notification uses full resolution
What is the difference between IP address and physical address
What else can an ordinary person do besides working in a factory to make money?
three. JS create cool snow effect
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
Imitate the choice of enterprise wechat conference room
Set the route and optimize the URL in thinkphp3.2.3
Odoo集成Plausible埋码监控平台
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
PHP实现执行定时任务的几种思路详解
MySQL数据库基本操作-DQL-基本查询
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
How to determine whether the checkbox in JS is selected