如何使块级元素垂直居中?

发布于 2024-09-11 08:47:23 字数 246 浏览 2 评论 0原文

假设我有这样的:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴骨ら 2024-09-18 08:47:23

这是经常被要求做的事情。没有简单的方法可以进行垂直对齐。有 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.

归属感 2024-09-18 08:47:23

我知道这是一个老问题,但是对于任何在寻找类似问题的解决方案时偶然发现这个问题的人,我编写了一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文