对长表格单元格进行自动换行,同时保持其他表格单元格的动态宽度

发布于 2024-10-09 23:58:37 字数 526 浏览 0 评论 0原文

我有一张表格,其中一个单元格中有一条长线。我需要将长线分开,这样就不会导致表格宽度超过 100%。我发现通过添加table-layout:fixedword-wrap:word-break,它会包裹长单元格。但是,使用 table-layout 的副作用是它会导致所有列具有相同的宽度。

您可以在此处查看示例:

http://jsfiddle.net/RYdLd/2/

如何我可以使第一列的宽度自动调整大小以仅适合其内容吗? (即在本例中,它的宽度应该足以显示该列中的 12。)

表中的数据将动态加载,因此解决方案哪个硬编码宽度值不好,因为无法提前知道列应该有多宽。我唯一的选择是使用 ,我不能使用

或其他元素。

I have a table which has a long line in one of its cells. I need the long line to be split so that it doesn't cause the table to be more than 100% wide. I found that by adding table-layout: fixed and word-wrap: word-break, it will wrap the long cell. However, a side effect of using table-layout is that it causes all columns to have the same width.

You can see an example of that here:

http://jsfiddle.net/RYdLd/2/

How can I make the first column's width auto size to fit only its contents? (i.e. In this example, it should be just wide enough to show the 1 and 2 in that column.)

The data in the table will be loaded dynamically, so a solution which hard codes width values is not good because there's no way to know in advance how wide a column should be. My only option is to use a <table>, I can't use a <div> or some other element.

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

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

发布评论

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

评论(4

恋竹姑娘 2024-10-16 23:58:37

根据官方规范,当您使用固定table-layout,第一行的列宽决定了整个表格的列宽。如果没有定义它们,它将均匀分布列宽。

由于似乎没有任何其他选项,我最终使用了以下方法:

  1. table-layout 设置为 auto 时加载表中的数据。
  2. 读取列的宽度我希望是动态的。
  3. 将这些列宽设置为其当前值。
  4. table-layout更改为固定。

这是一个不完美的示例(宽度减少了一点):

http://jsfiddle.net/ RYdLd/7/

According to the official specification, when you use a fixed table-layout, the first row's column widths determine the entire table's column widths. If none of them are defined, it will distribute the column widths evenly.

Since there doesn't seem to be any other option, I ended up using the following method:

  1. Loading the data in the table while the table-layout is set to auto.
  2. Reading the width of the columns I want to be dynamic.
  3. Setting those column widths to their current values.
  4. Changing the table-layout to fixed.

Here's an example which isn't perfect (the width gets decreased by a bit):

http://jsfiddle.net/RYdLd/7/

原来分手还会想你 2024-10-16 23:58:37

我在解决同样的问题时发现了这一点:

在元素上设置断词完全对应于在该元素内包含的文本的每个字符之间插入零宽度空格。

除了这实际上适用于正常的动态表!

该解决方案非常快,因为它不需要任何 JavaScript。

(但是,如果需要,可以从 Javascript 使用它。查找所有带有断词的单元格,抓取所有子文本节点,并在每个字符之间插入零宽度空格。即使如此,脚本在页面加载期间也只会运行一次,所以这应该仍然具有极高的性能。)

零宽度空间是

I discovered this while wrestling with the same problem:

Setting break-word on an element corresponds exactly to inserting a zero-width space between every character of the text contained inside that element.

Except that this actually works with normal, dynamic tables!

This solution is very fast, since it does not require any Javascript.

(It could however be used from Javascript if desired. Find all cells with break-word, grab all the child text nodes, and insert a zero-width space between every character. Even then, the script would run only once during page load, so this should still be extremely performant.)

Zero-width space is

一念一轮回 2024-10-16 23:58:37

您可以使用:word-wrap css样式来打破长句子。

word-wrap: break-word

You can use: word-wrap css style to break the long sentences.

word-wrap: break-word
诗化ㄋ丶相逢 2024-10-16 23:58:37

DIV固定表格 (table-layout:fixed) 中处理/换行长单词很容易 - 只需应用 CSS3 word -wrap:断词

在上面的动态表中,属性只完成了一半的工作。我们还需要帮助浏览器找到断点。

更详细的解释可以在文章在动态表格中换行长单词中找到。

It's easy to handle/wrap long words in DIVs and fixed tables (table-layout:fixed) - just apply CSS3 word-wrap:break-words.

Within dynamic tables above property does only half of the work. We need additionally to help the browsers find break points.

A bit more detailed explanation can be found in the article Wrap long words within dynamic tables.

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