读书人

四行CSS实现表格内容超过一行的部分

发布时间: 2013-03-01 18:33:02 作者: rapoo

4行CSS实现表格内容超过一行的部分,用省略号代替
Html代码

  1. table{
  2.   table-layout: fixed;
  3. }
  4. td{
  5.   white-space: nowrap;
  6.   overflow: hidden;
  7.   text-overflow: ellipsis;
  8. }

原理:

本方法用于解决表格单元格内容过多时的美观问题,主要涉及到4句CSS样式:

1. table-layout: fixed 由于table-layout的默认值是auto,即table的宽高将取决于其内容的多寡,如果内容的体积无法估测,那么最终表格的呈现形式也无法保证了,fixed一下就好了。(注意:此样式是关键)

2. white-space: nowrap 是为了保证无论单元格(TD)中文本内容有多少,都不会自动换行,此时多余的内容会在水平方向撑破单元格。

3. overflow: hidden 隐藏超出单元格的部分。

4. text-overflow: ellipsis 将被隐藏的那部分用省略号代替。


效果图:

四行CSS实现表格内容超过一行的部分,用省略号代替

源代码:Html代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <style type="text/css">
  5. table
  6. {
  7. table-layout: fixed;
  8. width: 100%;
  9. }
  10. td
  11. {
  12. white-space: nowrap;
  13. overflow: hidden;
  14. text-overflow: ellipsis;
  15. background-color: #ccc;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <table>
  21. <thead>
  22. <th>
  23. 第一列
  24. </th>
  25. <th>
  26. 第二列
  27. </th>
  28. </thead>
  29. <tbody>
  30. <tr>
  31. <td>
  32. <span>超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容 超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容</span><span>超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容 超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容</span>
  33. </td>
  34. <td>
  35. 超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容
  36. </td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </body>
  41. </html>

兼容性:http://www.php100.com/manual/css3_0/text-overflow.shtml

读书人网 >CSS

热点推荐