gpt4 book ai didi

html - 在文本 blob 中保留换行符但限制为一行

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:42 25 4
gpt4 key购买 nike

我有一个表,表中的每一行都有一个描述,该描述可能是这样的:

This is an example:
- Example 1
- Example 2
- Example 3

现在我想将描述限制为 1 行,上面有 overflow: 隐藏,并保留换行符,这样无论 - Example 1 是什么都不会显示。

如果可能的话,我想用纯 CSS + HTML 来做这件事。

我已经尝试过以下方法:

.overflowing-description {
overflow: hidden;
text-overflow: ellipsis;
white-space: pre-line;
max-height: 30px;
max-width: 20%;
}

max-width 和 max-height 都没有做任何事情,所以我假设是 white-space: pre-line 在这里覆盖了一些东西。

jsbin 示例: https://jsbin.com/nowuxot

th, td {
padding: 15px;
text-align: left;
border: 1px solid #ddd;
}
th {
border-bottom: 1px solid;
}
table {
width: 100%;
border-collapse: collapse;
}

.overflowing-description {
text-overflow: ellipsis;
max-height: 20px;
overflow: hidden;
white-space: pre-line;
max-width:5%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<table>
<thead>
<tr>
<th>
name
</th>
<th>
price
</th>
<th>
description
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Example
</td>
<td>
123
</td>
<td class="overflowing-description">
asd asd asd asd
asd
asd
asd
</td>
</tr>
<tr>
<td>
Example
</td>
<td>
123
</td>
<td class="overflowing-description">
asd asd asd asd
asd
asd
asd
</td>
</tr>
<tr>
<td>
Example
</td>
<td>
123
</td>
<td class="overflowing-description">
asd asd asd asd
asd
asd
asd
</td>
</tr>
</table>
</body>
</html>

最佳答案

一条评论 - 你不能改变 td 的高度。将文本放入p:

<td><p class="overflowing-description">asd asd asd asd</p></td>

其次,这足以隐藏下一行并保留换行符:

.overflowing-description {
max-height: 1rem;
overflow: hidden;
white-space: pre-line;
}

关于显示省略号,我曾经看到过用JavaScript自动添加的解决方案,但也有一些纯CSS的可能性。参见 that link .

更新

还有一个“草稿”CSS 属性可以做到这一点,它似乎得到了很好的支持:-webkit-line-clamp

更多更多 MDN .

关于html - 在文本 blob 中保留换行符但限制为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56948682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com