为什么我的 html 表“最大宽度”不会导致文本换行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2658895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does my html table 'max-width' not cause text to wrap?
提问by George2
I have an html table and one column (or <td>
) contains very long words. I want to set the max-width of the column so that if text is longer than the max-width the text should auto-wrap to next line. I have tried to set the css max-width
style on the appropriate <td>
element with "max-width:100px"
, but it does not work. The text is still very long and does not auto-wrap to next line.
我有一个 html 表,其中一列(或<td>
)包含很长的单词。我想设置列的最大宽度,以便如果文本比最大宽度长,文本应该自动换行到下一行。我尝试max-width
在适当的<td>
元素上设置 css样式"max-width:100px"
,但它不起作用。文本仍然很长,不会自动换行到下一行。
Any ideas what is wrong or any solution reference code? I am using IE 8 on Windows 7.
任何想法有什么问题或任何解决方案参考代码?我在 Windows 7 上使用 IE 8。
采纳答案by BalusC
You need to add the CSS3 property word-wrap: break-word;
.
您需要添加 CSS3 属性word-wrap: break-word;
。
回答by Ondross
You can put a div inside your table cells.
您可以在表格单元格中放置一个 div。
Nothing within that div will stretch out the table cell.
该 div 中的任何内容都不会拉伸表格单元格。
Works great in IE8 and Chrome at least.
至少在 IE8 和 Chrome 中运行良好。
回答by S.gfx
Old html td width will work in IE 8 for that... But you want maximum-width, not a width... besides the extra html attribute needed for each cell.
旧的 html td 宽度将在 IE 8 中工作......但是你想要最大宽度,而不是宽度......除了每个单元格所需的额外 html 属性。
回答by tjcoder
<td nowrap="wrap" style="width:100px;"></td>
All browsers..
所有浏览器..
Unfortunately you have to handle each cells content to keep the whole table fixed width. There is a width attribute for but it doesn't control anything..
不幸的是,您必须处理每个单元格的内容以保持整个表格的宽度固定。有一个宽度属性,但它不控制任何东西..