简体   繁体   English

表格宽度 100%,带溢出滚动条

[英]Table Width 100% with Overflow Scroll

I have tables on my web page set to width:100% .我的 web 页面上的表格设置为width:100% This stretches the table to the full width of my page if needed, or causes the contents of the table to wrap if the page is too narrow.如果需要,这会将表格拉伸到我页面的整个宽度,或者如果页面太窄则导致表格内容换行。 Everything in the table is kept in a grid.表格中的所有内容都保存在网格中。

Unfortunately, some issues arise if there are long words in the table which prevents the td from getting narrower.不幸的是,如果表中有长词会阻止 td 变窄,则会出现一些问题。 When this happens, the page starts to developing a horizontal scroll bar to accommodate the table, put other elements on the page, such as paragraphs, stay the width of the original page.发生这种情况时,页面开始开发一个水平滚动条以容纳表格,将页面上的其他元素(例如段落)保持在原始页面的宽度。

I tried to set the table to overflow:scroll to prevent the webpage from getting wider, limiting it to just the table, but this didn't fix the issue.我试图将表格设置为overflow:scroll以防止网页变宽,将其限制为仅表格,但这并没有解决问题。 I even tried overflow:hidden to see if it would respond, but I couldn't get it to work.我什至尝试过overflow:hidden看它是否会响应,但我无法让它工作。

The overflow would work if I set the table to display:block , but I can't get the table rows and table data cells to stretch across the full table and maintain its table-like appearance.如果我将表格设置为display:block ,溢出会起作用,但我无法让表格行和表格数据单元格延伸到整个表格并保持其类似表格的外观。

Is it possible to always have a table be 100% the width of a page, and to use overflow scroll if the table exceeds this width?是否可以始终让表格的宽度为页面宽度的 100%,并在表格超过此宽度时使用溢出滚动?

I added this example below.我在下面添加了这个例子。 It works nicely except when the page gets very narrow, like around 150px for this example.它工作得很好,除非页面变得非常窄,比如本例中的 150px 左右。 The table cells get so narrow that it can't wrap any more.表格单元格变得很窄,无法再换行。 I'd like for the table to overflow, but instead the whole page overflows.我希望表格溢出,但整个页面都溢出了。

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra. Fusce sit amet nisi sed ante semper porta non in velit. Aenean euismod finibus nibh id mollis. Duis finibus, urna eu gravida posuere, nisl tortor condimentum dui, sed suscipit ante lacus et augue. Vestibulum ut leo hendrerit, convallis lectus quis, mollis purus. Mauris vitae libero aliquam, porttitor nisi vitae, pellentesque odio. Mauris vestibulum lacinia sem. Aenean sit amet nulla ipsum. Mauris pellentesque dui feugiat odio feugiat, vel egestas eros pellentesque. Cras porttitor metus eu porttitor lacinia.</p>
<table style="width:100%;overflow:scroll;background:#555">
<tr><td>column one</td><td>column two</td><td>column three</td></tr>
</table>

You can't do it by changing display alone for the table, but there are a couple of ways you can achieve this.您不能通过单独更改表格的display来实现这一点,但有几种方法可以实现这一点。

1. Add a "scrolling" container div 1.添加一个“滚动”容器div

You can up the standard table in a container div and give it the overflow: auto;您最多可以在一个div容器标准表,并给overflow: auto; , so it will have the scroll bar. ,所以它会有滚动条。

table          { width: 100%; }
.scrollwrapper { overflow: auto; }

This means the table will stretch to at least 100% of the width even if it is not wide enough on its own, and if the content makes it wider than 100% them the div gets a scrollbar.这意味着表格将至少拉伸到宽度的 100%,即使它本身不够宽,并且如果内容使其宽度超过 100%,则 div 会得到一个滚动条。

Working Example:工作示例:

 table { width: 100%; border: 1px solid blue; } .scrollwrapper { overflow: auto; } td { border: 1px solid #aaa; }
 <h3>Scrolling Wrapper - Wide Table</h3> <div class="scrollwrapper"> <table> <tr> <td>thisisoneverylongwordthatwillnotwrapinthetablecell</td> <td>thisisoneverylongwordthatwillnotwrapinthetablecell</td> <td>thisisoneverylongwordthatwillnotwrapinthetablecell</td> </tr> <tr> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.</td> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.</td> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.</td> </tr> </table> </div> <h3>Scrolling Wrapper - Narrow Table</h3> <div class="scrollwrapper"> <table> <tr> <td>Column 1</td> <td>Column 2</td> <td>Column 3</td> </tr> <tr> <td>Short text</td> <td>Short text</td> <td>Short text</td> </tr> </table> </div>

2. Table with display:block 2. 带 display:block 的表格

This doesn't do what you want, but lets look at why...这不会做你想要的,但让我们看看为什么......

When you give the table display:block property, it adds the scrollbar to tables if they are wider than the available space, which is what you want.当您提供表格display:block属性时,如果表格比可用空间宽,它会向表格添加滚动条,这正是您想要的。 However as you noted, if the table isn't as wide as the available space, width:100% doesn't make the columns stretch to fill the width:但是,正如您所指出的,如果表格没有可用空间那么宽,则width:100%不会使列拉伸以填充宽度:

Working Example of display:block on tables display:block on table 的工作示例

 table { width: 100%; border: 1px solid red; overflow: auto; display: block; } td { border: 1px solid #aaa; }
 <h3>Display:Block - Wide Table</h3> <table> <tr> <td>thisisoneverylongwordthatwillnotwrapinthetablecell</td> <td>thisisoneverylongwordthatwillnotwrapinthetablecell</td> <td>thisisoneverylongwordthatwillnotwrapinthetablecell</td> </tr> <tr> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.</td> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.</td> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.</td> </tr> </table> <h3>Display:Block - Narrow Table</h3> <table> <tr> <td>Column 1</td> <td>Column 2</td> <td>Column 3</td> </tr> <tr> <td>Short text</td> <td>Short text</td> <td>Short text</td> </tr> </table>

As we can see in the example above, the table is actually stretching to 100%, however because the table row and cells don't fill the table.正如我们在上面的例子中看到的,表格实际上拉伸到了 100%,但是因为表格行和单元格没有填满表格。 This is when I discovered the tbody hack...这是我发现tbody hack 的时候...

3. Table with Display:block and tbody "hack" 3. 带有 Display:block 和 tbody "hack" 的表格

Note that this is very much a hack and I've no idea how well supported it might be!请注意,这在很大程度上是一种黑客行为,我不知道它的支持程度如何!

While playing around with this, I realised that the problem is that the rows and cells are not acting as normal rows and cells because they are not in a parent with display:table .在玩这个时,我意识到问题在于行和单元格不能作为正常的行和单元格,因为它们不在具有display:table的父级display:table

That's when I discovered that when I added display:table to the tbody , it gave the rows and cells a parent with the correct display type and it worked!那时我发现当我将display:table添加到tbody ,它为行和单元格提供了具有正确显示类型的父级并且它起作用了!

table       { display: block; overflow: auto; }
table tbody { display: table; width: 100%; }

It seems to work even when the tbody is not explicitly added in the html.即使tbody没有明确添加到 html 中,它似乎也能工作。

Working Example of tbody hack tbody hack 的工作示例

 table { border: 1px solid blue; overflow: auto; display: block; } table tbody { display: table; width: 100%; } td { border: 1px solid #aaa; }
 <h3>Tbody "Hack" - Wide Table</h3> <table> <tr> <td>thisisoneverylongwordthatwillnotwrapinthetablecell</td> <td>thisisoneverylongwordthatwillnotwrapinthetablecell</td> <td>thisisoneverylongwordthatwillnotwrapinthetablecell</td> </tr> <tr> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.</td> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.</td> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.</td> </tr> </table> <h3>Tbody "Hack" - Narrow Table</h3> <table class="tbody-hack"> <tr> <td>Column 1</td> <td>Column 2</td> <td>Column 3</td> </tr> <tr> <td>Short text</td> <td>Short text</td> <td>Short text</td> </tr> </table>

Would this work?这行得通吗? The overflow will pop in if the width of the table is greater than the width of the <div>如果表格的宽度大于<div>的宽度,则会弹出溢出

 <div id="table" style="width:200px"> <table style="width:100%; overflow:auto;display:block"> <tr> <th>header</th> <th>header</th> <th>header</th> <th>header</th> <th>header</th> <th>header</th> </tr> <tr> <td>body</td> <td>body</td> <td>body</td> <td>body</td> <td>body</td> <td>body</td> </tr> </table> </div>

Using CSS grid.使用 CSS 网格。

 .table-wrapper { display: grid; grid-template-columns: repeat(1, minmax(0, 1fr)); overflow: auto; white-space: nowrap; }
 <div class="table-wrapper"> <table> <thead> <tr> <th>Head 1</th> <th>Head 2</th> <th>Head 3</th> <th>Head 4</th> <th>Head 5</th> <th>Head 1</th> <th>Head 2</th> <th>Head 3</th> <th>Head 4</th> <th>Head 5</th> <th>Head 1</th> <th>Head 2</th> <th>Head 3</th> <th>Head 4</th> <th>Head 5</th> <th>Head 1</th> <th>Head 2</th> <th>Head 3</th> <th>Head 4</th> <th>Head 5</th> </tr> </thead> <tbody> <tr> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> <td>Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> <td>Content 5</td> </tr> </tbody> </table> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM