Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
css 垂直居中的几种方法_csstransform属性,希望能够帮助你!!!。
/* HTML */ <div class='father'> <div class='son'></div> </div> <style> .father { display: table-cell; vertical-align: middle; width: 300px; height: 300px; border: 3px solid red; } .son { width: 50px; height: 50px; background-color: aqua; } </style> 复制代码
<!-- HTMl --> <div class="father"> <div class="son"></div> </div> <style> .father { position: relative; width: 300px; height: 300px; border: 3px solid red; } .son { position: absolute; background-color: aqua; width: 50px; height: 50px; top: 0; bottom: 0; margin: auto; } </style> 复制代码
<!-- HTMl --> <div class="father"> <div class="son"></div> </div> <style> .father { position: relative; width: 300px; height: 300px; border: 3px solid red; } .son { position: absolute; width: 100px; height: 100px; background-color: aqua; top: 50%; /* 负margin须是高度的一半 */ margin-top: -50px; } </style> 复制代码
<!-- HTMl --> <div class="father"> <div class="son"></div> </div> <style> .father { position: relative; width: 300px; height: 300px; border: 3px solid red; } .son { position: absolute; width: 100px; height: 100px; background-color: aqua; /* 注意"-"两边要隔开 减去的须是高度的一半*/ top: calc(50% - 50px); } </style> 复制代码
<!-- HTMl --> <div class="father"> <div class="son"></div> </div> <style> .father { position: relative; width: 300px; height: 300px; border: 3px solid red; } .son { position: absolute; width: 100px; height: 100px; background-color: aqua; top: 50%; transform: translateY(-50%); } </style> 复制代码
<!-- HTMl --> <div class="father"> <div class="son"></div> </div> <style> .father { width: 300px; height: 300px; border: 3px solid red; line-height: 300px; } .son { background-color: aqua; width: 100px; height: 100px; display: inline-block; vertical-align: middle; } </style> 复制代码
<!-- HTMl --> <div class="father"> <div class="son"></div> </div> <style> .father { width: 300px; height: 300px; border: 3px solid red; display: flex; align-items: center; } .son { background-color: aqua; width: 100px; height: 100px; } </style> 复制代码
<!-- HTMl --> <div class="father"> <div class="son"></div> </div> <style> .father { width: 300px; height: 300px; border: 3px solid red; display: grid; } .son { background-color: aqua; width: 100px; height: 100px; align-self: center; } </style> 复制代码
<!-- HTMl --> <div class="father"> <div class="son"></div> </div> <style> .father { width: 300px; height: 300px; border: 3px solid red; display: block; } .father::after { content: ""; display: inline-block; vertical-align: middle; height: 100%; } .son { background-color: aqua; width: 50px; height: 50px; display: inline-block; vertical-align: middle; } </style> 复制代码
<!-- HTML --> <div class="father"> <div class="hide"></div> <div class="son"></div> </div> <style> .father { width: 300px; height: 300px; border: 3px solid red; } .son { background-color: aqua; width: 50%; height: 50%; } .hide { width: 50px; height: 25%; } </style> 复制代码
<!-- HTML --> <div class="father"> <div class="son"></div> </div> <style> .father { width: 300px; height: 300px; border: 3px solid red; writing-mode: vertical-lr; text-align: center; } .son { background-color: aqua; width: 100px; height: 100px; writing-mode: horizontal-tb; display: inline-block; } </style> 复制代码
作者:soloplayer
链接:https://juejin.cn/post/
来源:掘金
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章