【发布时间】:2016-04-27 13:22:34
【问题描述】:
我正在尝试以表格格式显示各种项目(每个条目都有各种属性)。还需要以下内容:
- 列应该是动态的,以便紧密贴合内容,没有固定宽度(由表格布局处理)
- 行应该可以点击以激活对该行条目的操作,例如显示具有该内容的扩展版本的新页面(使用 CSS
display: table/table-row/table-cell而不是<table>/<tr>/<td>处理 - 根据示例代码)。 (我的另一个原因是我使用 JSF,它生成 HTML,但有点让我坚持使用它。) -
我无法做到这一点:如果可能,表格不应超出屏幕宽度。特别是,“摘要”列显示(可能很长)文本,必要时将被截断,因此文本适合单元格,该单元格适应表格宽度,表格宽度可能不大于屏幕宽度。
- 我在此摘要中使用 CSS
text-overflow: ellipsis以及其他需要的设置。因为周围的<div>已经有了display: table-cell,而ellipsis属性needs an inline/block element,“摘要”文本被包裹在另一个<div>中。
- 我在此摘要中使用 CSS
虽然这是我目前能够实现的(不理想)(请注意底部的滚动条 - 表示表格在右侧的窗口外运行):
这是实现这一点的准系统代码(可以省略所有标有/*l&f*/(外观)的属性,它们只是为了生成一个看起来更干净、更容易调试的示例)。
CSS 和 HTML:
A {
/*l&f*/text-decoration: inherit;
/*l&f*/color: inherit;
}
.list-table {
display: table;
/*l&f*/border: 1px solid blue;
/*l&f*/border-spacing: 5px;
}
.list-tr {
display: table-row;
/*l&f*/background-color: lightgray;
}
.list-td {
display: table-cell;
white-space: nowrap; /* one line */
/*l&f*/padding: 2px; border : 1px solid green;
/*l&f*/border: 1px solid red;
}
.summary {
display: block;
white-space: nowrap; /* one line */
overflow: hidden; /* make text overflow */
text-overflow: ellipsis; /* truncate tex with ... */
/*l&f*/background-color: lightblue;
}
<div class="list-table">
<a href="#1" class="list-tr">
<div class="list-td">Row 1</div>
<div class="list-td">More stuff</div>
<div class="list-td">
<div class="summary">Some single line run on text goes here</div>
</div>
</a>
<a href="#2" class="list-tr">
<div class="list-td">Row 2</div>
<div class="list-td">Other column</div>
<div class="list-td">
<div class="summary">This is text content that runs on and on
and on without end and may need to be truncated somewhere on the
summary screen depending on screen size to show only the first part
that will fit.</div>
</div>
</a>
<a href="#3" class="list-tr">
<div class="list-td">Row 3</div>
<div class="list-td">Still other content</div>
<div class="list-td">
<div class="summary">Here is still more long text that may
need truncation in the summary version because it is so long.</div>
</div>
</a>
</div>
我已经能够使用display: -moz-box(以及各种其他-moz- 设置)以summary 样式实现所需的结果。不高兴,因为这不是跨平台的。
你有更好的建议吗?非常感谢您的帮助:-)
【问题讨论】:
-
你可以使用other box stylings和mox-box来获得你的跨浏览器兼容性
-
@Aravona,在这种情况下似乎有点冒险,因为其他浏览器的“实验”功能不对应完全,也已被
flex取代,这又有所不同.... 不过会看看flexbox是否带来任何快乐,目前还不熟悉。 -
这主要是因为您已经在尝试 moz-box 这只是浏览器兼容性的第一次成功。可能还有更多 - 只是谷歌:)