CSS实现table固定宽度,超过单元格部分内容省略

<table>单元格的宽度是根据内容的大小自适应的,没有内容的地方就挤到了一起。需要固定表格宽度和每一列的宽度。

table-layout:fixed

在固定表格布局中,水平布局仅取决于表格宽度、列宽度、表格边框宽度、单元格间距,而与单元格的内容无关。

white-space: nowrap; text-overflow: ellipsis; overflow: hidden;

超过指定长度的文本以省略号的形式显示。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<style>
* {
    padding: 0;
    margin: 0;
}
table {
    width: 300px;
    table-layout:fixed;
}
.first_col {
    white-space: nowrap; text-overflow: ellipsis; overflow: hidden;
    width: 150px;
}
.first_col_text {
    white-space: nowrap; text-overflow: ellipsis; overflow: hidden;
    display: inline-block;
    color: red;
    width: 120px;
}
img {
    width: 20px;
    height: 20px;
    margin-right: 5px;
}
.second_col {
    white-space: nowrap; text-overflow: ellipsis; overflow: hidden;    
    width: 100px;
}
.third_col{
    white-space: nowrap; text-overflow: ellipsis; overflow: hidden;
    width: 50px;
}
</style>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td class="first_col">
        <span class="first_col_text">123456789012345</span>
        <img src="http://tb2.bdstatic.com/tb/editor/images/face/i_f23.png?t=20131111">
    </td>
    <td class="second_col">12345678901234567890123456789012345678901234567890</td>
    <td class="third_col">1</td>
  </tr>
  <tr>
    <td class="first_col"></td>
    <td class="second_col"></td>
    <td class="third_col">12345678901234567890123456789012345678901234567890123456789012345678901234567890</td>
  </tr>
  <tr>
    <td class="first_col">123456789012345</td>
    <td class="second_col"></td>
    <td class="third_col"></td>
  </tr>
</table>
</body>
</html>

 

 显示效果:

 

转载于:https://www.cnblogs.com/wenruo/p/7641425.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值