html table overflow,html - Table Width 100% with Overflow Scroll - Stack Overflow

You can't do it by changing display alone for the table, but there are a couple of ways you can achieve this.

1. Add a "scrolling" container div

You can up the standard table in a container div and give it the 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.

Working Example:

table {

width: 100%;

border: 1px solid blue;

}

.scrollwrapper {

overflow: auto;

}

td {

border: 1px solid #aaa;

}

Scrolling Wrapper - Wide Table

thisisoneverylongwordthatwillnotwrapinthetablecellthisisoneverylongwordthatwillnotwrapinthetablecellthisisoneverylongwordthatwillnotwrapinthetablecell
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.

Scrolling Wrapper - Narrow Table

Column 1Column 2Column 3
Short textShort textShort text

2. Table with 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. 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:

Working Example of display:block on tables

table {

width: 100%;

border: 1px solid red;

overflow: auto;

display: block;

}

td {

border: 1px solid #aaa;

}

Display:Block - Wide Table

thisisoneverylongwordthatwillnotwrapinthetablecellthisisoneverylongwordthatwillnotwrapinthetablecellthisisoneverylongwordthatwillnotwrapinthetablecell
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.

Display:Block - Narrow Table

Column 1Column 2Column 3
Short textShort textShort text

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. This is when I discovered the tbody hack...

3. Table with Display:block and 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.

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!

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.

Working Example of tbody hack

table {

border: 1px solid blue;

overflow: auto;

display: block;

}

table tbody {

display: table;

width: 100%;

}

td {

border: 1px solid #aaa;

}

Tbody "Hack" - Wide Table

thisisoneverylongwordthatwillnotwrapinthetablecellthisisoneverylongwordthatwillnotwrapinthetablecellthisisoneverylongwordthatwillnotwrapinthetablecell
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus nulla et lorem imperdiet, eu malesuada enim viverra.

Tbody "Hack" - Narrow Table

Column 1Column 2Column 3
Short textShort textShort text
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值