扫码关注官方订阅号
首先,line-height肯定不行了,增加标签清除浮动法必须得是块级元素?绝对定位可以吗?应该怎么写呢?感谢各位博学的大大花时间解答!!~~
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
方法还是挺多的,我写了2种:
html代码
<p class="container-wrapper"> <p class="container"> <p class="center"> </p> </p> </p>
css代码
.container-wrapper{ width:400px; height:300px; } .container{ width:100%; height:100%; border:1px solid violet; position:relative; } .center{ width:100px; height:100px; position:absolute; margin:auto; left:0px; top:0px; right:0px; bottom:0px; border:1px solid green; }
html代码同上css代码
.container-wrapper{ width:400px; height:300px; } .container{ width:100%; height:100%; border:1px solid violet; position:relative; } .center{ border:1px solid green; width:100px; height:100px; position:absolute; left:50%; top:50%; transform:translate(-50%, -50%); -moz-transform:translate(-50%, -50%); -ms-transform:translate(-50%, -50%); -o-transform:translate(-50%, -50%); -webkit-transform:translate(-50%, -50%);
http://www.cnblogs.com/crystalmoore/p/5041522.html 提供了4种方法1.利用vertical-align:middle
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> body{margin: 0;} .father{width: 100%;height: 100%;position: fixed;left: 0;top: 0;text-align: center;font-size: 0;/*设置font-size为了消除代码换行所造成的空隙*/background:rgba(0,0,0,0.5); } .daughter{vertical-align: middle;}/*实现daughter居中*/ .son{vertical-align: middle;display:inline-block;height: 100%;} </style> </head> <body> <p class = "father"> <p class="son"></p> <img class = "daughter" src="1.jpg" alt="我要居中"> </p> </body> </html>
2.使用transform实现
<style> body{margin: 0;} .father {position: absolute; left:50%; top:50%; -webkit-transform:translate(-50%,-50%); transform:translate(-50%,-50%);background:rgba(0,0,0,0.5); } </style> <p class="father"> <img src="1.jpg"> </p>
3.弹性盒模型
<style> body{margin: 0;} .father{position:fixed; width:100%; height:100%; background:rgba(0,0,0,0.5); left:0px; top:0px;display:flex;justify-content:center;align-items:center;} </style> <p class="father"> <img src="1.jpg"> </p>
4.用表格布局
<style> *{margin:0px; padding:0px;} table {position:absolute; width:100%; height:100%; text-align:center; background:rgba(0,0,0,0.5);} .daughter {display:inline-block;} </style> <table> <tr> <td> <p class="daughter"> <img src="1.jpg"> </p> </td> </tr> </table>
html,body{width: 100%;height: 100%;} .box{width: 300px;height:50%;overflow: hidden;margin: 200px auto;background: #DAC8A7;border-radius: 10px;} .item{width: 50px;height: 50px;border-radius: 50%;background: #4A4439;display: block;} .box { display: flex; align-items: center;/**居中**/ flex-direction:column; justify-content:center;/**平均分**/ } <p class="box"> <span class="item"></span> </p>
有一种方法是父元素display为tablecell,子元素为inline-block;还有一种是父元素是用flex-box,items:center来实现垂直居中。
可以使用translate手机回答 代码从略
父元素高度width: 20%;
子元素 设置绝对定位 top:50%;tranlateY:-50%;
translate的位移是针对这个子元素的-50%
所以可以做到垂直居中
点击看看,目前所见总结得最全的垂直居中
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
方法还是挺多的,我写了2种:
一
html代码
css代码
二
html代码同上
css代码
http://www.cnblogs.com/crystalmoore/p/5041522.html 提供了4种方法
1.利用vertical-align:middle
2.使用transform实现
3.弹性盒模型
4.用表格布局
有一种方法是父元素display为tablecell,子元素为inline-block;还有一种是父元素是用flex-box,items:center来实现垂直居中。
可以使用translate
手机回答 代码从略
父元素高度
width: 20%;
子元素 设置绝对定位
top:50%;
tranlateY:-50%;
translate的位移是针对这个子元素的-50%
所以可以做到垂直居中
点击看看,目前所见总结得最全的垂直居中