gpt4 book ai didi

html - 如何在不使用显示 : block; overflow-x: auto? 的情况下创建水平滚动表

转载 作者:行者123 更新时间:2023-12-02 20:57:18 25 4
gpt4 key购买 nike

我正在构建一个包含大量表格的应用程序。有些在水平方向上很长,所以如果它们延伸到页面之外,我想为它们添加水平滚动。

我在谷歌上搜索了一个解决方案,他们都指向使用 display: block; overflow-x: auto; 在 table 上。这适用于比屏幕宽的表格,但现在表格被设置为 display:block; 而不是默认的 display: table; 表格单元格都被压在一起并且表格不会保持其自然的 100% 宽度,这使得不比屏幕宽的表格看起来很糟糕并且被压在页面的左侧。

似乎我既可以让列数较少的表看起来不错,也可以让列数很多的表不在页面上,或者列数较少的表看起来很糟糕,而列数很多的表很好且响应迅速。但似乎没有同时适用于两者的解决方案。

表格是用户生成的,所以我无法自己手动为每个表格设置 css。

那么有没有一种方法可以让 display: table 真正地保持在一个表格上,这样它的行为就像一个表格,并且如果表格比页面宽,它可以水平滚动?

更新:

JS fiddle :

With display: block; overflow-x: auto

Without display: block; overflow-x: auto

抱歉,CSS 在 html 中,但用户将使用所见即所得的编辑器来制作表格,这就是它在应用程序中的方式。

最佳答案

使用容器 <div>包装表格并为该 div 应用溢出属性。

.container{
display:block;
overflow-x: auto;
}
<div class="container">
<table style="width: 100%;" border="1">
<thead>
<tr>
<th style="text-align: center;">head</th>
<th style="text-align: center;">head</th>
<th style="text-align: center;">head</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 33.3333%; text-align: center;">text</td>
<td style="width: 33.3333%; text-align: center;">text</td>
<td style="width: 33.3333%; text-align: center;">text</td>
</tr>
</tbody>
</table>
</div>

<br/><br/>

<div class="container">
<table style="width: 100%;" border="1">
<thead>
<tr>
<th style="text-align: center;">head</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-beerus"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45152-v1474530116-dragon-ball-z-dokkan-battle-awakening-medals-bog-beerus-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-goten"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45176-v1474530282-dragon-ball-z-dokkan-battle-awakening-medals-bog-goten-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-trunks"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45184-v1474530347-dragon-ball-z-dokkan-battle-awakening-medals-bog-trunks-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-ultimate-gohan"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45168-v1474530235-dragon-ball-z-dokkan-battle-awakening-medals-bog-ultimate-gohan-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-vegeta"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45192-v1474530403-dragon-ball-z-dokkan-battle-awakening-medals-bog-vegeta-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-videl"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45200-v1474530470-dragon-ball-z-dokkan-battle-awakening-medals-bog-videl-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-whis"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45160-v1474530187-dragon-ball-z-dokkan-battle-awakening-medals-bog-whis-image.png" style="width: 70px;"></a>
<br>
</td>
</tr>
</tbody>
</table>
</div>
<br/><br/>
<div class="container">
<table style="width: 100%;" border="1">
<thead>
<tr>
<th style="text-align: center;">head</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
<th style="text-align: center;">head
<br>
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-beerus"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45152-v1474530116-dragon-ball-z-dokkan-battle-awakening-medals-bog-beerus-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-goten"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45176-v1474530282-dragon-ball-z-dokkan-battle-awakening-medals-bog-goten-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-trunks"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45184-v1474530347-dragon-ball-z-dokkan-battle-awakening-medals-bog-trunks-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-ultimate-gohan"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45168-v1474530235-dragon-ball-z-dokkan-battle-awakening-medals-bog-ultimate-gohan-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-vegeta"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45192-v1474530403-dragon-ball-z-dokkan-battle-awakening-medals-bog-vegeta-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-videl"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45200-v1474530470-dragon-ball-z-dokkan-battle-awakening-medals-bog-videl-image.png" style="width: 70px;"></a>
<br>
</td>
<td style="width: 14.2857%; text-align: center;">
<a href="https://guidir.com/guides/dragon-ball-z-dokkan-battle/awakening-medals/bog-whis"><img class="fr-dib fr-draggable" src="https://commondatastorage.googleapis.com/guidir/item-value-images/thumb_45160-v1474530187-dragon-ball-z-dokkan-battle-awakening-medals-bog-whis-image.png" style="width: 70px;"></a>
<br>
</td>
</tr>
</tbody>
</table>
</div>

关于html - 如何在不使用显示 : block; overflow-x: auto? 的情况下创建水平滚动表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39824031/

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