gpt4 book ai didi

javascript - 如何制作 "Table",其中行在 HTML5/css3 中水平换行?

转载 作者:太空狗 更新时间:2023-10-29 15:23:21 28 4
gpt4 key购买 nike

我有下表,使用了 html table、tr、td 标签。 enter image description here

一切都很好,但我使用的是 Angular ,问题是如果我有更多行,我就没有垂直空间。相反,我想“溢出”到它的右边,这样它看起来像:

enter image description here

使用 HTML5/CSS 的最佳方式是什么,这样我就可以使内容在超出表格时溢出?请注意,在某些情况下,我可能有 2 或 3 个条目,所以我希望我不必在开始时将宽度设为正常大小的两倍,而是根据我拥有的条目数来调整表格的大小在表中。没有滚动条。

我觉得 HTML 中的表格标签有限制,可能不允许我这样做。做这个的最好方式是什么?使用 div?

<table style="background:green;">
<tr><td>Name</td><td>Sport</td><td>Score</td></tr>
<tr><td>Jordan</td><td>Soccer</td><td>50</td></tr>
<tr><td>Jordan</td><td>Soccer</td><td>50</td></tr>
<tr><td>Jordan</td><td>Soccer</td><td>50</td></tr>
</table>

尝试使用 flexbox 方法,尽管看起来如果我将 flexbox 放在 div 中,那么一旦条目超出第一列,背景似乎不会正确填充:

https://jsfiddle.net/t0h7w2hw/

最佳答案

请按照下面的代码示例

这将在移动设备中以列表样式换行。您需要为每个内容 td 设置数据标签属性,以便它知道它是哪一列。在移动设备中,您可以使用 css content:""属性在 td 之前显示标签。

body {
font-family: "Arial", sans-serif;
line-height: 1.25;
}
table {
border: 1px solid #ccc;
border-collapse: collapse;
margin: 0;
padding: 0;
width: 100%;
table-layout: fixed;
}
table caption {
font-size: 1.5em;
margin: .5em 0 .75em;
}
table tr {
background: #f8f8f8;
border: 1px solid #ddd;
padding: .35em;
}
table th,
table td {
padding: .625em;
text-align: center;
}
table th {
font-size: .85em;
letter-spacing: .1em;
text-transform: uppercase;
}
@media screen and (max-width: 600px) {
table {
border: 0;
}
table caption {
font-size: 1.3em;
}
table thead {
border: none;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
table tr {
border-bottom: 3px solid #ddd;
display: block;
margin-bottom: .625em;
}
table td {
border-bottom: 1px solid #ddd;
display: block;
font-size: .8em;
text-align: right;
}
table td:before {
/*
* aria-label has no advantage, it won't be read inside a table
content: attr(aria-label);
*/
content: attr(data-label);
float: left;
font-weight: bold;
text-transform: uppercase;
}
table td:last-child {
border-bottom: 0;
}
}
<table>
<caption>Statement Summary</caption>
<thead>
<tr>
<th scope="col">Card</th>
<th scope="col">Due Date</th>
<th scope="col">Amount</th>
<th scope="col">Period</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Card">Visa - 3412</td>
<td data-label="Due Date">04/01/2016</td>
<td data-label="Amount">$1,190</td>
<td data-label="Period">03/01/2016 - 03/31/2016</td>
</tr>
<tr>
<td scope="row" data-label="Card">Visa - 6076</td>
<td data-label="Due Date">03/01/2016</td>
<td data-label="Amount">$2,443</td>
<td data-label="Period">02/01/2016 - 02/29/2016</td>
</tr>
<tr>
<td scope="row" data-label="Card">Corporate AMEX</td>
<td data-label="Due Date">03/01/2016</td>
<td data-label="Amount">$1,181</td>
<td data-label="Period">02/01/2016 - 02/29/2016</td>
</tr>
<tr>
<td scope="row" data-label="Card">Visa - 3412</td>
<td data-label="Due Date">02/01/2016</td>
<td data-label="Amount">$842</td>
<td data-label="Period">01/01/2016 - 01/31/2016</td>
</tr>
</tbody>
</table>

关于javascript - 如何制作 "Table",其中行在 HTML5/css3 中水平换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43947285/

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