[英]how to set word break in table th or td, when column and table-layout auto using CSS
我希望这很简单。
我想为表保留table-layout:auto,因为这可以使“名称”列的大小合适,而url列会占用剩余的空间。
问题是当url列变大时,表的大小会自动超过宽度。
理想情况下,一旦溢出超出所包含的div的大小,我想将其隐藏在“ url”列上。
在Jsfiddle示例中,您可以看到第一个表的大小很好,“名称”列占用了可用空间。
在第二个表中,由于“ url”列中的文本,它超出了其包含的div。 我不能锻炼如何隐藏此溢出。
<!DOCTYPE html>
<html>
<head>
<title>Table Test</title>
<style>
table {
table-layout:auto;
}
table, th, td
{
text-align:left;
white-space:nowrap;
text-overflow:ellipsis;
}
</style>
</head>
<div style="width:400px; border: 1px solid red; background-color:lightgrey;">
<table>
<thead>
<tr><th>Status</th><th style="width:100%">Name</th><th style="width:100%">Url</th></tr>
</thead>
<tbody>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred long long long name</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com/xxxxxx </td></tr>
</tbody>
</table>
</div>
</div>
<p/>
<div style="width:400px; border: 1px solid red; background-color:lightgrey;">
<table>
<thead>
<tr><th>Status</th><th style="width:100%">Name</th><th style="width:100%">Url</th></tr>
</thead>
<tbody>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred long long long name</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com</td></tr>
<tr><td>On</td><td>Fred</td><td>http://www.google.com/xxxxxxxxxxxxxxxxxxxxxxx</td></tr>
</tbody>
</table>
</div>
</body>
</html>
您的div包装器正在显示溢出。 添加隐藏的溢出。 小提琴
div{
overflow: hidden;
}
如果您想在悬停时显示完整的网址,可以这样尝试:
小提琴- 小提琴链接!
的HTML
<td>
<span>http://reallylongstring.com</span>
</td>
的CSS
span {
width: 200px;
overflow: hidden;
display: block;
}
span:hover {
overflow: initial;
}
由于您为div
了固定的宽度,因此只需添加overflow:hidden;
样式,并且不会显示表格中的溢出内容。
JSFiddle: http : //jsfiddle.net/ginovva320/CFbKR/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.