简体   繁体   English

如何垂直放置块级元素?

[英]How to center a block level element vertically?

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. margin-*属性指示表在其父元素内居中。

Is there a similar way to center the element vertically? 有没有类似的方法可以使元素垂直居中?

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. vertical-align属性,但是它用于内联元素的行对齐。 Check out this link for different ways to do vertical-centering. 查看此链接 ,了解进行垂直居中的不同方法。

Edit: Here's another good link to get you started. 编辑: 这是另一个让您入门的好链接。

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. 我知道这是一个古老的问题,但是对于任何偶然发现这个问题的人,我已经编写了一个jQuery插件,可以很容易地将任何块元素居中。 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 . 有一个链接,并演示在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM