HTML 和 CSS - 垂直对齐 div
我的左侧有一个 div,右侧有文本。文本可以是 2 行或 3 行长,我希望左侧 div 垂直居中于该文本。我在网上查了一下,似乎所有不使用表格来做到这一点的方法都很复杂。有没有简单的方法可以做到这一点?
大多数建议似乎都指向 http://www.jakpsatweb.cz/ css/css-vertical-center-solution.html 使用“CSS 表”。 CSS 表格是正确的选择吗?这真的比仅使用 HTML 表格更好吗?
I have a div on the left hand side, and text on the right hand side. The text can be either 2 or 3 lines long, and I want the left hand div to be vertically centered to that text. I've looked online and it seems that all the ways to do this without using tables are complicated. Is there an easy way to do this?
Most suggestions seem to point to http://www.jakpsatweb.cz/css/css-vertical-center-solution.html which uses "CSS tables". Are CSS tables the way to go? Is that really better than just using HTML tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,CSS 表格比 HTML 表格更好,因为它们不暗示任何语义,而是明确用于描述表示。
但是,对于您的情况,CSS 的
vertical-align
可以工作。例如:http://jsbin.com/itoyo3/edit。Yes, CSS tables are better than HTML tables because they do not imply any semantic meaning but are explicitly used to describe presentation.
However, for your case CSS's
vertical-align
works. For example: http://jsbin.com/itoyo3/edit.你可以用绝对定位来做到这一点;
将代码更改为如下所示:
内容
像这样设置你的CSS:
You could do it with absolute positioning;
Change your code to look like this:
<div id="right-content"><div id="the-left-hand-div">whatever goes here</div><p>The content</p></div>
Set your css up like this: