---
title: "4行CSS实现表格内容超过一行的部分，用省略号代替(转载)"
author: "javaEEdevelop"
date: "2013-02-25T17:25:56Z"
tags: []
categories: ["Web前端"]
slug: "4-css"
draft: false
lastmod: "2013-02-25T17:25:56Z"
id: 1814825
url: "https://javaeedevelop.iteye.com/blog/1814825"
---

Html代码  [![收藏代码](http://jackjobs.iteye.com/images/icon_star.png)](/admin/blogs/ "收藏这段代码")

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 将被隐藏的那部分用省略号代替。

 

* * *

## 效果图：

![](http://pic002.cnblogs.com/images/2012/377582/2012050715122618.png "点击查看原始大小图片")

## 源代码：

Html代码  [![收藏代码](http://jackjobs.iteye.com/images/icon_star.png)](/admin/blogs/ "收藏这段代码")

01. &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
02. &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
03. &lt;head&gt;
04. &lt;style type="text/css"&gt;
05. table
06. {
07. table-layout: fixed;
08. width: 100%;
09. }
10. td
11. {
12. white-space: nowrap;
13. overflow: hidden;
14. text-overflow: ellipsis;
15. background-color: #ccc;
16. }
17. &lt;/style&gt;
18. &lt;/head&gt;
19. &lt;body&gt;
20. &lt;table&gt;
21. &lt;thead&gt;
22. &lt;th&gt;
23. 第一列
24. &lt;/th&gt;
25. &lt;th&gt;
26. 第二列
27. &lt;/th&gt;
28. &lt;/thead&gt;
29. &lt;tbody&gt;
30. &lt;tr&gt;
31. &lt;td&gt;
32. &lt;span&gt;超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容 超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容&lt;/span&gt;&lt;span&gt;超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容 超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容&lt;/span&gt;
33. &lt;/td&gt;
34. &lt;td&gt;
35. 超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容
36. &lt;/td&gt;
37. &lt;/tr&gt;
38. &lt;/tbody&gt;
39. &lt;/table&gt;
40. &lt;/body&gt;
41. &lt;/html&gt;

 

        兼容性：[http://www.php100.com/manual/css3\_0/text-overflow.shtml](http://www.php100.com/manual/css3_0/text-overflow.shtml)