使用CSS,垂直对齐短标题和描述的最优雅的方法是什么,而不是使用表格

发布于 2024-10-01 05:36:27 字数 494 浏览 1 评论 0原文

不使用表格
必须在 IE 7 和 Firefox、Chrome、Safari 中工作

垂直对齐的最简单方法:

A Title (20px)
With a variable length description (12px), can be 1 line or 2 (when wrapped)

在宽度为 500px、高度为 60px 的 div 内,可能是使用一张桌子。 不使用表格,最优雅的方法是什么?

请不要简单地给出“固定”高度解决方案的链接作为答案,因为它可以具有可变的高度。

更新:示例和可编辑测试:http://jsfiddle.net/mn2CH/3/

Not using table and
must work in IE 7 and Firefox, Chrome, Safari

The easiest way to vertically align this:

A Title (20px)
With a variable length description (12px), can be 1 line or 2 (when wrapped)

inside a div that has a width 500px, and height 60px, is probably by using a table. Without using a table, what is the most elegant way to do it?

Please do not simply give a link to a "fixed" height solution as the answer, as this can have variable height.

Update: sample and editable test: http://jsfiddle.net/mn2CH/3/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

☆獨立☆ 2024-10-08 05:36:27

看看这个:

使用 CSS 垂直居中

方法 2 (使用负边距)是“经典”方法,我已经使用过几次。

Take a look to this:

Vertical centering with CSS

The Method 2 (using negative margins) is the "classic" approach, I have used it a couple of times.

伤痕我心 2024-10-08 05:36:27

The cleanest approach I've seen that supports older versions of IE is this method. Basically, for IE, it uses a clever method of having your main container, in your case it would be 500px by 60px, and then two inner containers - one is positioned relatively with top: 50% and the other being relatively positioned with top: -50%. Intuitively, one container is positioned half way down the main container, and then the other container is moved up by half it's own height.

And of course, for newer browsers, you can use the newer display attributes on arbitrary elements, which the above article also explains.

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