使div居中是最不可能的
1. 使用 flexbox 居中flexbox 它是一种垂直和水平内容的现代方式:
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
<div class="container">
<div class="centered-div">centered content</div>
</div>
css grid 还可以居中内容:
.container {
display: grid;
place-items: center;
height: 100vh;
}
<div class="container">
<div class="centered-div">centered content</div>
</div>
您可以使用绝对定位 div 居中:
.container {
position: relative;
height: 100vh;
}
.centered-div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="container">
<div class="centered-div">centered content</div>
</div>
对简单的水平居中,请使用 margin: auto:
.centered-div {
width: 50%;
margin: 0 auto;
}
<div class="centered-div">centered content</div>
内联元素或内联元素:
.container {
text-align: center;
line-height: 100vh; /* full viewport height for vertical centering */
}
.centered-div {
display: inline-block;
vertical-align: middle;
line-height: normal;
}
<div class="container">
<div class="centered-div">centered content</div>
</div>
以上就是如何让div居中?详情请关注其他相关文章!
相册删照不用慌,三步恢复全攻略!
如何让div居中?-css教程
关键数据守护,备份恢复至最新,安心无忧!
了解 CSS 框架-js教程
探索sex8地址web3389,揭秘未知领域!
JavaScript - ray 有氧运动第 2 天-js教程
超值抢购!香港云服务器,仅需10元年费!
了解 CSS 框架-js教程
JavaScript - ray 有氧运动第 2 天-js教程
使用 Sista AI 的 React AI ChatBot 解锁智能对话-js教程
使用 MongoDB、Django、Celery 和 Sendgrid 构建批量通知-js教程
哪种C 框架最适合多线程和并发移动开发?-C
哪种C 最适合云原生Web应用程序开发的框架?-C
如何将Golang框架与MongoDB集成?-Golang
如何将Golang框架与Redis结合使用?-Golang
在大型项目中,golang框架的团队合作方便吗?-Golang
C在敏捷开发中 框架与C 库-C
商业C 框架与C 库的区别-C
跨平台C 框架与C 库的适用范围-C