简体   繁体   English

如何在具有溢出隐藏的div中垂直对齐文本?

[英]How to vertically align a text in a div which has overflow hidden?

I have a gridView in asp which will generate a table. 我在ASP中有一个gridView,它将生成一个表。 As I need to show only fixed number of lines in a TD looking around I came to decision to place a div inside a td so then I could achieve my goal. 由于我只需要在TD周围显示固定数量的行,就决定将div放置在td中,这样我就可以实现我的目标。

table.XDataGridView td div.inner-table-div {
height: 32px;
padding: 1px;
overflow: hidden;
display: block;
}

.rfTaskExecutionWrapper td {
    min-width: 100px;
    font-size: 12px;
}

这是当前行为

I want that first column to be vertically aligned on center, same for second column. 我希望第一列在中心垂直对齐,第二列相同。 I also wish to show only 2 or 3 lines of text. 我也只希望显示2或3行文字。 For that I've limited the height though it seems that I still see some text of third line - which I don't want. 为此,尽管似乎我仍然看到第三行的一些文字,但我还是不想限制它的高度。

The solution has to work on chrome and on Internet Explorer 7(on windows CE). 该解决方案必须能够在chrome和Internet Explorer 7(在Windows CE上)上运行。

Vertically aligning the content of a table cell can be done using vertical-align: middle on your td. 垂直对齐表格单元格的内容可以使用td上的vertical-align: middle来完成。

If you want to make sure there's only two lines visible in your second colomn, use line-height. 如果要确保第二个列中仅可见两行,请使用line-height。 The height of your div is 32 pixels. div的高度为32像素。 you have a padding of 1 pixel on the top and the bottom so that leaves 30 pixels. 您的顶部和底部的填充为1像素,因此剩下30像素。 (asuming you're using the standard box-sizing) That means that if you add line-height: 15px to your div, you will only see two lines and no trailing third line. (假设您使用的是标准框大小),这意味着如果您在div中添加line-height: 15px ,则只会看到两行,而没有尾随的第三行。

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

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