css
主页 > 网页 > css >

table设置超出部分隐藏,鼠标移上去显示全部内容的方法

2023-01-01 | 佚名 | 点击:

table内容超出宽度时隐藏并显示省略标记

HTML中,一个表格,要达到二个条件:

1、内容多了不自动换行;

2、固定单元格宽度。如果内容超出,则隐藏;

如 果在IE下,只是写成<table style="table-layout:fixed; overflow:hidden;"><tr><td nowrap>则可,而在FF下则不行。兼容IE和FF的写法,应该为:<table style="table-layout:fixed;"><tr>td style="overflow:hidden;" nowrap>

3、显示省略标记,只支持IE:

text-overflow:ellipsis;

测试代码:

<style>.tbl {table-layout:fixed}.td {overflow:hidden;text-overflow:ellipsis}</style>

<table class="tbl" border=1 width=80>

<tr>

    <td width=25% class="td" nowrap>abcdefghigklmnopqrstuvwxyz 1234567890</td>

    <td class="td" nowrap><div>abcdefghigklmnopqrstuvwxyz 1234567890</div></td>

</tr>

</table>

table设置超出部分隐藏,鼠标移上去显示全部内容

核心代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

table {

    border-collapse: collapse;

    width:55em;

    table-layout:fixed;/* 只有定义了表格的布局算法为fixed,下面td的定义才能起作用。 */

}

.thead th{

    width: 63px;

    height: 25px;

    text-align: center;

}

  

table td{

    position: relative;

    /*width: 80px;*/

    height: 25px;

    text-align: center;

    font-size: 12px;

    font-weight: normal;

  

    width:100%;

    word-break:keep-all;/* 不换行 */

    white-space:nowrap;/* 不换行 */

    overflow:hidden;/* 内容超出宽度时隐藏超出部分的内容 */

    text-overflow:ellipsis;/* 当对象内文本溢出时显示省略标记(...) ;需与overflow:hidden;一起使用。*/

}

table td:hover {

    overflow: visible;  /* 鼠标放上去显示全部文字 */

}

比较适合单独的页面,去过是全站模板就不能这么用了

table表格溢出隐藏

CSS部分:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

table{

    table-layout:fixed;

    width:100%;

    height:100%;

    border-collapse:collapse;

}

table td{

    border:1px solid #5a5858;

    overflow:hidden;

    text-overflow:ellipsis;

    white-space:nowrap;

}

.box{

    width:400px;

    height:200px;

}

HTML部分:

1

2

3

4

5

6

7

8

<div class="box">

    <table>

        <tr>

            <td>1111</td>

            <td>5555555555555555555456464645646464646</td>

        </tr>

    </table>

</div>

原文链接:
相关文章
最新更新