
如何在 css 中将 div 居中
使div居中是最不可能的事情
1. 使用 flexbox 居中
flexbox 是一种垂直和水平居中内容的现代方式:
.container { display: flex; justify-content: center; align-items: center; height: 100vh;}
centered content
2. 网格居中
css grid 还可以居中内容:
.container { display: grid; place-items: center; height: 100vh;}
centered content
3. 绝对定位居中
您可以使用绝对定位将 div 居中:
.container { position: relative; height: 100vh;}.centered-div { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);}
centered content
4. 使用 margin auto 居中
对于简单的水平居中,请使用 margin: auto:
.centered-div { width: 50%; margin: 0 auto;}
centered content
5. 使用 margin auto 居中
对于内联或内联块元素:
.container { text-align: center; line-height: 100vh; /* full viewport height for vertical centering */}.centered-div { display: inline-block; vertical-align: middle; line-height: normal;}
centered content
以上就是如何让div居中?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1629412.html
微信扫一扫
支付宝扫一扫