编辑:如果之前有人问过这个问题或者这是一个众所周知的技术,我深表歉意。我不知道这会叫什么,因此我无法搜索它。
在我的 HTML 文档中,我垂直居中的内容如下:
<div style='position: absolute; margin: auto; top: 0; bottom: 0; height: 500px'>
<!-- CONTENT HERE -->
</div>
这很好用,唯一的问题是代码不太可重用,因为它取决于 block 的特定高度。我想做的是这样的:
<div class='vcenter(500px)'>
<!-- CONTENT HERE -->
</div>
.vcenter(height)
{
position: absolute;
margin: auto;
top: 0;
bottom: 0;
height: height;
}
如果我可以实现这样的功能,那么每次我想要垂直居中时就不必重复代码。 CSS 有实现这个的方法吗?
最佳答案
你不满意这样做吗?
<div class='vcenter' style='height: 500px'>
<!-- CONTENT HERE -->
</div>
关于css - 类似函数的 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7464451/