前言
最近在总结前端知识,也参加了一些面试,面试中遇到一道题要求垂直居中,现在对这进行一下总结,也好巩固一下知识。
css实现垂直居中
1.利用line-height实现居中,这种方法适合纯文字类的;
<!-- css --> <style> .parents { height: 400px; line-height: 400px; width: 400px; border: 1px solid red; text-align: center; } .child { background-color: blue; color: #fff; } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child">css布局,实现垂直居中</span> </div> </body>
2.通过设置父容器相对定位,子级设置绝对定位,标签通过margin实现自适应居中;
<!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; position: relative; } .child { width: 200px; height: 100px; line-height: 100px; text-align: center; color: #fff; background-color: blue; /* 四个方向设置为0, 然后通过margin为auto自适应居中 */ position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child">css布局,实现垂直居中</span> </div> </body>
3.弹性布局flex 父级设置display: flex; 子级设置margin为auto实现自适应居中;
<!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; display: flex; } .child { width: 200px; height: 100px; line-height: 100px; text-align: center; color: #333; background-color: yellow; margin: auto; } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child">css布局,实现垂直居中</span> </div> </body>
4. 父级设置相对定位,子级设置绝对定位,并且通过位移transform实现;
<!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; position: relative; } .child { width: 200px; height: 100px; line-height: 100px; text-align: center; color: #fff; background-color: green; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child">css布局,实现垂直居中</span> </div> </body>
5. 父级设置弹性盒子,并设置弹性盒子相关属性;
<!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; display: flex; justify-content: center; /* 水平 */ align-items: center; /* 垂直 */ } .child { width: 200px; height: 100px; color: black; background-color: orange; } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child"></span> </div> </body>
6. 网格布局,父级通过转换成表格形式,然后子级设置行内或行内块实现。(需要注意的是:vertical-align: middle使用的前提条件是内联元素以及display值为table-cell的元素)。
<!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; display: table-cell; text-align: center; vertical-align: middle; } .child { width: 200px; height: 100px; color: #fff; background-color: blue; display: inline-block; /* 子元素设置行内或行内块 */ } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child"></span> </div> </body>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。
navigator.mediaDevices.getUserMedia 应项目要求,需要实现移动端app嵌入H5页面...
本文在研究了使用非对称加密保障数据安全的 技术基础 上,使用 NodeJS 作为服务...
今天在制作网页特效的过程中遇到了这样的问题,浏览器中预览首选参数中指定的浏...
磨刀霍霍向前冲【DW技巧】用Dreamweaver 制作可视化表格,很简单,如下文所示。 ...
首先,在发贴之前!再一次感谢我要自学网!虽然我现在所会的东西不是全部在自学...
大家可能还记得在过去的文章我们我们曾经介绍我们收集的前端开发人员必备的工具...
Redis 内部使用一个 RedisObject 对象来表示所有的 key 和 value,RedisObject ...
随着公司影响力的壮大和产品的不断提升,相关的形象设计要逐步跟得上,需要逐渐...
以实例说明Dreamwaver CS 6版本的应用。即使现在用很多有关DreamwaverCS6版本的...
众所周知,JS 是一门单线程语言,可是浏览器又能很好的处理异步请求,那么到底是...