简体   繁体   English

水平滚动容器内的 flexbox 宽度

[英]flexbox width inside horizontally scrollable container

I am trying to create a layout for a virtual table.我正在尝试为虚拟表创建布局。 A virtual table will be placed inside .table-body element.一个虚拟表将被放置在.table-body元素中。 Each table cell has the same flex layout styles as cells in .table-header so with the same parent element with they would look the same.每个表格单元格与.table-header中的单元格具有相同的 flex 布局样式,因此具有相同的父元素,它们看起来相同。 The problem is that the .table-cell elements do not stretch .table-header width, and .table-header element does not stretch the .table-container width.问题是.table-cell元素不会拉伸.table-header宽度,而.table-header元素不会拉伸.table-container宽度。

I'm trying to get the .table-cell 's to give the width for the .table-header and .table-container .我试图让.table-cell给出.table-header.table-container的宽度。 And the .table-body would take up the remaining space in the .table-container.table-body会占用.table-container的剩余空间

Here is Codesandbox to play with.这是可以玩的Codesandbox

 .table-wrapper { width: 500px; height: 300px; overflow-x: scroll; background: rgba(255, 255, 0, 0.2); } .table-container { display: flex; flex-flow: column nowrap; width: 100%; height: 100%; /* If you uncomment next line, you see what I'm trying to achieve */ /* min-width: 1100px; */ background: rgba(0, 255, 0, 0.3); } .table-header { display: flex; flex-flow: row nowrap; width: 100%; flex: 0 0 auto; background: rgba(255, 100, 0, 0.3); } .table-cell { min-width: 100px; display: flex; flex: 1 1 auto; height: 30px; line-height: 30px; border: 1px solid blue; } .table-body { display: flex; flex: 1 1 auto; border: 1px solid red; background: rgba(255, 0, 0, 0.3); }
 <div class="table-wrapper"> <div class="table-container"> <div class="table-header"> <div class="table-cell" style="flex: 0 0 180px;">header-cell 1</div> <div class="table-cell">header-cell 2</div> <div class="table-cell">header-cell 3</div> <div class="table-cell">header-cell 4</div> <div class="table-cell" style="flex: 0 0 200px;">header-cell 5</div> <div class="table-cell">header-cell 6</div> <div class="table-cell">header-cell 7</div> </div> <div class="table-body">virtual table here</div> </div> </div>

Consider the use of inline-flex instead of flex and define the width using width and not flex-basis考虑使用inline-flex而不是flex并使用width而不是flex-basis定义宽度

 .table-wrapper { width: 500px; height: 300px; overflow-x: scroll; background: rgba(255, 255, 0, 0.2); } .table-container { display: inline-flex; /* UPDATED */ flex-flow: column nowrap; min-width: 100%; /* UPDATED */ height: 100%; background: rgba(0, 255, 0, 0.3); } .table-header { display: flex; flex-flow: row nowrap; width: 100%; flex: 0 0 auto; background: rgba(255, 100, 0, 0.3); } .table-cell { min-width: 100px; display: flex; flex: 1 1 auto; height: 30px; line-height: 30px; border: 1px solid blue; } .table-body { display: flex; flex: 1 1 auto; border: 1px solid red; background: rgba(255, 0, 0, 0.3); }
 <div class="table-wrapper"> <div class="table-container"> <div class="table-header"> <div class="table-cell" style="flex: 0 0 180px;width:180px;">header-cell 1</div> <div class="table-cell">header-cell 2</div> <div class="table-cell">header-cell 3</div> <div class="table-cell">header-cell 4</div> <div class="table-cell" style="flex: 0 0 200px;width:200px;">header-cell 5</div> <div class="table-cell">header-cell 6</div> <div class="table-cell">header-cell 7</div> </div> <div class="table-body">virtual table here</div> </div> </div>

Here's a workaround I found, use width: min-content;这是我找到的解决方法,使用width: min-content; for both table-header and table-container.对于表头和表容器。
This only work when you get rid of the inline style in the table-cell .这仅在您摆脱table-cell中的内联样式时才有效。

 .table-wrapper { width: 500px; height: 300px; overflow-x: scroll; background: rgba(255, 255, 0, 0.2); } .table-container { display: flex; flex-flow: column nowrap; width: min-content; height: 100%; /* If you uncomment next line, you see what I'm trying to achieve */ /* min-width: 1100px; */ background: rgba(0, 255, 0, 0.3); } .table-header { display: flex; flex-flow: row nowrap; width: min-content; flex: 0 0 auto; background: rgba(255, 100, 0, 0.3); } .table-cell { min-width: 100px; display: flex; flex: 1 1 auto; height: 30px; line-height: 30px; border: 1px solid blue; } .table-body { display: flex; flex: 1 1 auto; border: 1px solid red; background: rgba(255, 0, 0, 0.3); }
 <div class="table-wrapper"> <div class="table-container"> <div class="table-header"> <div class="table-cell">header-cell 1</div> <div class="table-cell">header-cell 2</div> <div class="table-cell">header-cell 3</div> <div class="table-cell">header-cell 4</div> <div class="table-cell" >header-cell 5</div> <div class="table-cell">header-cell 6</div> <div class="table-cell">header-cell 7</div> </div> <div class="table-body">virtual table here</div> </div> </div>

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

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