首页 / CSS / 6种CSS水平垂直居中解决方案
6种CSS水平垂直居中解决方案
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了6种CSS水平垂直居中解决方案,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2024字,纯文字阅读大概需要3分钟。
内容图文
本文主要和大家介绍了CSS水平垂直居中解决方案(6种)的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。准备
创建元素
<p class="parent"> <p class="child">child</p> </p>
垂直水平居中方案一:知道宽度的情况下 absolute+margin负值
.parent { width:400px; height:400px; background: red; position: relative; } .child { position: absolute; left:50%; top:50%; background: yellow; width:50px; height:50px; margin-left:-25px; margin-top:-25px; }
垂直水平居中方案二:不知道宽高的情况下 absolute+transform
.parent { width:400px; height:400px; background: red; position: relative; } .child { position: absolute; left:50%; top:50%; transform: translate(-50%,-50%); }
垂直居中方案三:position+margin:auto
.parent { position:relative; width:200px; height:200px; background: red; } .child { width:80px; height:40px; background: yellow; position: absolute; left:0; top:0; right:0 ; bottom:0; margin:auto; }
垂直居中方案四:+ 多行文本的垂直居中 :table-cell+vertical-align:middle;
.parent { height: 300px; width:400px; border: 1px solid red; display: table-cell; vertical-align: middle; text-align: center; } .child { display: inline-block; width:50px; height:50px; background: blue; } /* 或者 */ .parent { width: 400px; height: 300px; display: table-cell; vertical-align: middle; border: 1px solid red; text-align: center; } .child { display: inline-block; vertical-align: middle; background: blue; }
垂直居中方案五:display: flex
.parent { width:400px; height:200px; background:red; display: flex; justify-content:center; align-items:center; } .child { height:100px; width:100px; background:green; }
垂直居中方案六:伪元素
.parent { width:200px; height:200px; background:red; text-align: center; } .child { height:100px; width:100px; background:yellow; display: inline-block; vertical-align: middle; } .parent:before { content:""; height:100%; vertical-align: middle; display: inline-block; }
相关推荐:
css水平垂直居中的4种实现方法
html的元素水平垂直居中应该怎么设置
css如何实现水平垂直居中以及两端对齐的代码分享
以上就是6种CSS水平垂直居中解决方案的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的6种CSS水平垂直居中解决方案全部内容,希望文章能够帮你解决6种CSS水平垂直居中解决方案所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。