简体   繁体   English

在html表中设置max-height元素

[英]Set the max-height element in html table

I realise that this question has probably been asked to death, but none of the answers I have found seem to work. 我意识到这个问题可能已被要求死亡,但我找到的答案似乎都没有效果。 I have an ASP repeater control, in which I have set the ItemTemplate to have a simple table with two rows. 我有一个ASP转发器控件,我已经将ItemTemplate设置为一个包含两行的简单表。

When populating the rows, I want the rows to stay the height I have set them, not grow to the size of the content. 填充行时,我希望行保持我设置的高度,而不是增长到内容的大小。

I have tried setting style="max-height:100px; overflow:auto"; 我试过设置style="max-height:100px; overflow:auto"; on the table, table row and table data which did not do a thing. 在表上,表行和表数据没有做任何事情。

How do I ensure that my repeater template are always the same height, not the height of their content? 如何确保我的转发器模板始终具有相同的高度,而不是其内容的高度?

I think you need a solution like this css: 我想你需要像这样的css解决方案:

table{
  width:200px;
  table-layout:fixed;
}

tr{
   white-space:nowrap;   
}

td {
  height:20px;
  white-space:inherit;
  width:100px;
  overflow:hidden;
}

Fixed table layout: The horizontal layout only depends on the table's width and the width of the columns, not the contents of the cells. 固定表格布局:水平布局仅取决于表格的宽度和列的宽度,而不取决于单元格的内容。 Allows a browser to lay out the table faster than the automatic table layout. 允许浏览器比自动表格布局更快地布置表格。

white-space: nowrap: Sequences of whitespace will collapse into a single whitespace. white-space:nowrap:空格的序列将折叠成一个空格。 Text will never wrap to the next line. 文本永远不会换行到下一行。 The text continues on the same line until a 文本继续在同一行,直到a
tag is encountered 遇到标记

overflow: hidden: The overflow is clipped, and the rest of the content will be invisible overflow:hidden:溢出被剪切,其余内容将不可见

works in most modern browsers. 适用于大多数现代浏览器。

fiddle here 在这里小提琴

Your CSS should be { height: 100px; 你的CSS应该是{height:100px; max-height: 100px !important; max-height:100px!important; overflow: hidden; 溢出:隐藏; } }

  • first it will set the height 首先它将设置高度
  • then it will set the maximum height - you'll want to keep this the same. 然后它将设置最大高度 - 你想要保持这一点。 It will also tell the browser that this is more important than the previous setting of height. 它还会告诉浏览器这比以前的高度设置更重要。 You only set that to make sure IE stays in line, as IE8 and lower does not read max-height at all, or poorly at best. 你只设置它以确保IE保持一致,因为IE8和更低版本根本不读取最大高度,或者最多不能读取。
  • last, overflow should be "hidden", that way you'll hide the content. 最后,溢出应该是“隐藏”,这样你就会隐藏内容。 Unless that's not what you wanted to do? 除非那不是你想做的事情?

If you want scrollbars, I'm afraid a TD or TR isn't the best way to go. 如果你想要滚动条,我担心TD或TR不是最好的方法。 You need to put a div within each TD and set the above CSS to that instead. 你需要在每个TD中放一个div,然后将上面的CSS设置为。 In other words, you need more nesting. 换句话说,您需要更多嵌套。

Make sure you are applying those CSS properties (particularly overflow:hidden) to the repeating parent block element. 确保将这些CSS属性(特别是overflow:hidden)应用于重复的父块元素。 Doing it to a child element will not be helpful if the parent is expanded and doing it to an inline element will not affect it because inline elements necessarily use the size of their contents. 如果父元素被展开并且对内联元素执行它不会影响它,那么对子元素执行它将没有用,因为内联元素必然使用其内容的大小。

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

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