如何使块级元素垂直居中?
假设我有这样的:
<table style="margin-left: auto; margin-right: auto;">
<tr>
<td>
</td>
</tr>
</table>
margin-*
属性指示表格在其父元素中居中。
有类似的方法使元素垂直居中吗?
Say I have a like so:
<table style="margin-left: auto; margin-right: auto;">
<tr>
<td>
</td>
</tr>
</table>
The margin-*
attributes instruct the table to center itself within its parent element.
Is there a similar way to center the element vertically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是经常被要求做的事情。没有简单的方法可以进行垂直对齐。有
vertical-align
属性,但它用于内联元素的行对齐。查看此链接了解垂直居中的不同方法。编辑:这是另一个可以帮助您入门的好链接。
This is a very often requested thing to do. There's no simple way to do vertical alignment. There's the
vertical-align
attribute, but it's for line-alignment of an inline element. Check out this link for different ways to do vertical-centering.Edit: Here's another good link to get you started.
我知道这是一个老问题,但是对于任何在寻找类似问题的解决方案时偶然发现这个问题的人,我编写了一个 jQuery 插件,可以非常轻松地将任何块元素居中。这并不是为了推广我自己的项目,老实说,我只是想尽我所能提供帮助和贡献。 此处有一个链接和演示。
I know this is an old question, but for anyone who stumbles across this in search of a solution to a similar question, I've written a jQuery plugin that can center any block element very easily. This is not to promote my own project, I honestly just want to help out and contribute where I can. There's a link and demo here.