css常用元素水平垂直居中方案
发布时间:2019-08-09 16:28:20 作者:JinsongChai
我要评论

这篇文章主要介绍了css常用元素水平垂直居中方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
适用场景:父子宽高都可未知(比较推荐这种方式,简单,而且目前兼容性也不错。)
<html> <head> <style> .parent { width: 100%; height: 100px; background: cyan; display: flex; justify-content: center; align-items: center; } .son { width: 20%; height: 20%; background: pink; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
适用场景:父元素宽高已知未知都行,但是首先得有宽高。其次子元素的宽高必须已知,因为需要设置子元素的负margin. (也可以将负margin设置成translate来做位移实现)
<html> <head> <style> .parent { position: relative; width: 200px; height: 200px; background: pink; } .son { position: absolute; left: 50%; top: 50%; margin-left: -25px; margin-top: -25px; width: 50px; height: 50px; background: yellow; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
适用场景:父子元素宽高都未知的情况(该方式不需要明确知道子元素宽高,子元素宽高可用百分比,对于子元素宽高不确定需要居中的情况比较适合。)
<html> <head> <style> .parent { position: relative; width: 200px; height: 200px; background: cyan; } .son { position: absolute; left: 0; top: 0; bottom: 0; right: 0; margin: auto; width: 10%; height: 10%; background: yellow; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
适用场景:父子元素宽高未知,兼容性不大好
<html> <head> <style> .parent { display: grid; } .son { jusitify-self: center; align-self: center; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
Table-cell + text-align + vertical-align
适用场景: 父元素大小已知(非百分比高度),子元素大小未知,但子元素须为行内块元素,较好的兼容性
<html> <head> <style> .parent { display: table-cell; vertical-align: middle; text-align: center; width: 100vw; height: 90vh; background-color: yellowgreen; } .son { display: inline-block; width: 200px; height: 200px; background-color: Indigo; } </style> </head> <body> <div class='parent'> <div class='son'></div> </div> </body> </html>
适用场景:父子宽高都可未知,子元素需为行内块元素(这种方式其实就是使用伪元素的高度为100%,子元素和伪元素都设置 vertical-align: middle实现垂直居中的效果)
<html> <head> <style> .parent { height: 100vh; width: 100vw; text-align: center; background: #c0c0c0; } .parent:before { content: "\200B"; display: inline-block; height: 100%; vertical-align: middle; } .son { display: inline-block; vertical-align: middle; width: 200px; height: 200px; padding: 10px 15px; background: #f5f5f5; } </style> </head> <body> <div class="parent"> <div class="son"></div> </div> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持澳门金沙网上娱乐。
相关文章
- 这篇文章主要介绍了CSS水平垂直居中解决方案(6种)的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-02-10
- 这篇文章主要给大家介绍了css实现元素水平垂直居中的两种方式,文中给出了完整的示例代码供大家参考学习,对大家的学习或者工作具有一定的参考价值,有需要的朋友们下面来2017-04-23
- 这篇文章主要介绍了CSS水平垂直居中的几种方法总结,垂直居中是布局中十分常见的效果之一,本文介绍了几种方法,有兴趣的可以了解一下。2016-12-19
- 这篇文章主要为大家详细介绍了css让容器水平垂直居中的7种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-10-17
- 这篇文章给大家介绍了三个小节的内容,其中包括关于css3中flexbox需要掌握的概念、flexbox实现水平垂直居中对齐和三列等高自适应页脚区域黏附底部的布局,有需要的可以参考2016-09-12
- 下面小编就为大家带来一篇Flexbox制作CSS布局实现水平垂直居中的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-07-27
- 这篇文章主要介绍了CSS解决页面图片水平垂直居中问题的方法,文中给出了三种方案而无需依赖JavaScript,需要的朋友可以参考下2016-03-10
- 这篇文章主要介绍了使用CSS实现水平垂直居中效果的方法的总结,涵盖了从最原始的高度设置到令人兴奋的CSS3的Flexbox方式,非常全面,十分推荐!需要的朋友可以参考下2016-03-10
- 这篇文章主要介绍了CSS定位“十字架”之水平垂直居中的相关资料,CSS如何定位“十字架”实现水平垂直居中效果,小编为大家解答,感兴趣的小伙伴们可以参考一下2016-03-02
- 这篇文章主要介绍了通过CSS设置未知大小图片在已知大小容器水平垂直居中,需要的朋友可以参考下2014-05-22
最新评论