为什么 `margin: auto auto;` 不垂直居中 Div?
使用 margin:auto 垂直对齐 Div
而 margin: 0 auto;可以使div水平居中,margin: auto auto;未按预期垂直对齐。另外,垂直对齐:中间;对于块级元素无效。
为什么垂直自动边距失败
- margin-top: auto 和 margin-bottom: auto 计算为零,不提供居中效果。
- margin-top: -50% 计算其相对于包含块的宽度而不是高度的值。
使用嵌套元素的解决方法
一种可行的解决方法是嵌套三个元素:
.container { display: table; height: 100%; position: absolute; overflow: hidden; width: 100%; } .helper { position: absolute; top: 50%; display: table-cell; vertical-align: middle; } .content { position: relative; top: -50%; margin: 0 auto; width: 200px; border: 1px solid orange; }
登录后复制
<div class="container"> <div class="helper"> <div class="content"> <p>stuff</p> </div> </div> </div>
登录后复制
在此解决方案中:
- 外部 .container 元素建立一个类似表格的结构。
- .helper 元素放置垂直中点的内容。
- .content div 位于 .helper 内,可以水平居中,边距:0 auto;.
以上是为什么 `margin: auto auto;` 不垂直居中 Div?的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章
Windows 11 KB5054979中的新功能以及如何解决更新问题
4 周前
By DDD
如何修复KB5055523无法在Windows 11中安装?
3 周前
By DDD
Inzoi:如何申请学校和大学
1 个月前
By DDD
如何修复KB5055518无法在Windows 10中安装?
3 周前
By DDD
在哪里可以找到Atomfall中的站点办公室钥匙
4 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)