简体   繁体   English

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

[英]How can I create a horizontal scrolling table without using display: block; overflow-x: auto?

I'm building an app that has a lot of tables. 我正在构建一个包含大量表格的应用程序。 Some are horizontally long so I want to add horizontal scrolling to them if they extent outside of the page. 有些是横向长的,所以如果它们扩展到页面之外,我想向它们添加水平滚动。

I googled around for a solution and they all point to using display: block; overflow-x: auto; 我用google搜索解决方案,他们都指向使用display: block; overflow-x: auto; display: block; overflow-x: auto; on the table. 在桌子上。 This works for tables wider than the screen but now that tables are set as display:block; 这适用于比屏幕宽的表,但现在表被设置为display:block; instead of the default display: table; 而不是默认的display: table; the table cells are all squished together and the table doesn't maintain its natural 100% width which makes tables that aren't wider than the screen took horrible and squished to the left of the page. 表格单元格都被挤压在一起,桌子不会保持100%的自然宽度,这使得不比屏幕宽的桌子变得非常糟糕,并且在页面的左侧压扁。

It seems like I can either have tables with less columns look good and have tables with lots of columns go off the page, or have tables with less columns look bad and tables with lots of columns nice and responsive. 看起来我可以拥有较少列的表看起来很好并且有很多列的表离开页面,或者具有较少列的表看起来很糟糕并且具有许多列的表很好且响应性很强。 But there doesn't seem to be a solution for both. 但似乎两者都没有解决方案。

The tables are user generated so I cant manually set the css for each table myself. 这些表是用户生成的,所以我不能自己为每个表手动设置css。

So is there a way to actaully keep display: table on a table so it behaves like a table and have the table scroll horizontally if its wider than the page? 那么是否有一种方法可以保持display: table在桌子上,所以它的行为就像一张桌子,如果它比页面宽,表格会水平滚动吗?

Update: 更新:

JS fiddles: JS小提琴:

With display: block; 带显示:块; overflow-x: auto overflow-x:auto

Without display: block; 没有显示:块; overflow-x: auto overflow-x:auto

Sorry the CSS is in the html but users will be using WYSIWYG editors to make the tables so thats the way it will be in the app. 抱歉,CSS在html中,但用户将使用WYSIWYG编辑器来制作表格,以便它在应用程序中的方式。

Use container <div> to wrap tables and apply overflow property for that div. 使用容器<div>来包装表并为该div应用overflow属性。

 .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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在使用overflow-x:auto时在我的表和滚动条之间放置一个空格;“ - Is there a way I can place a space between my table and scrollbars when using overflow-x: auto;" CSS 使用 overflow-x 进行水平滚动导航栏时出现异常 - CSS anomaly when using overflow-x for horizontal scrolling navbar 溢出-x:iPhone 上的自动不滚动 - overflow-x: auto on iPhone is not scrolling 使用overflow-x:auto时,Internet Explorer不会调整表格单元格的高度(因此会隐藏内容)。 Chrome可以。 我怎样才能解决这个问题? - Internet Explorer doesn't adjust for table cell height when using overflow-x:auto (so it hides content). Chrome does. How can I fix this? 溢出-x:自动,不隐藏内容 - Overflow-x: auto, without hiding content “溢出-x:自动”不在 CSS 网格中的表上工作以使其可滚动以实现响应,我该如何解决? - "overflow-x: auto' not working on table in CSS Grid to make it scrollable for responsiveness, how do I fix? 溢出-x,以避免在浏览器中进行任何水平滚动 - overflow-x to avoid any horizontal scrolling in browser CSS:水平滚动(溢出-x:滚动)不工作 - CSS: Horizontal scrolling (overflow-x: scroll) Not Working 使用overflow-x:hidden删除IE 8-10中的水平滚动 - Remove horizontal scrolling in IE 8-10 using overflow-x:hidden 如何使用溢出X创建图例中的表 - How to create a table within a legend with overflow-x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM