【发布时间】:2015-10-19 12:53:33
【问题描述】:
我想在包含两个表的 DIV 上使用 CSS flexbox,并使用 flex-grow 使其中一个表填充可用空间。然而,它并没有增长。似乎这是因为表格不是块显示元素。如果我将 TABLE 包装在 DIV 中,它就可以工作。但是,我想知道如果没有额外的DIV,是否有办法让它工作?
以下是一个示例 - 第一个容器没有 DIVS,第二个容器带有 DIV,并且具有理想的布局。
div.container {
display: flex;
background-color: red;
}
#nodivs table:first-child {
background-color: green;
}
#nodivs table:last-child {
background-color: blue;
flex-grow: 1;
}
#divs div:first-child {
background-color: green;
}
#divs div:last-child {
background-color: blue;
flex-grow: 1;
}
#divs div:last-child table {
width: 100%
}
<div id="nodivs" class="container">
<table>
<thead>
<tr><th>T1C1</th><th>T1C2</th><th>T1C3</th></tr>
</thead>
</table>
<table>
<thead>
<tr><th>T2C1</th><th>T2C2</th><th>T2C3</th></tr>
</thead>
</table>
</div>
<br><br>
<div id="divs" class="container">
<div><table>
<thead>
<tr><th>T1C1</th><th>T1C2</th><th>T1C3</th></tr>
</thead>
</table></div>
<div><table>
<thead>
<tr><th>T2C1</th><th>T2C2</th><th>T2C3</th></tr>
</thead>
</table></div>
</div>
【问题讨论】:
-
您尝试将您的
<tables>设置为display:block;吗?您可能还必须为<tr>和<td>重置display。但那时我会把它包装在一个 div 中,哈哈。我也会尝试添加这种样式:div.container table { width: 100%; }用于nodivs版本 -
从语义上讲,应该不需要单独的 HTML 代码来同时具有 flex-box 质量和表格布局。我很好奇,为什么你有 2 个单独的表?将它们结合起来可能会解决您的问题。
-
jaunt - 实际上,这两张表我实际上是在显示两组不相关的数据。
-
zgood,如果我将表格设置为 display: block,其中的列将不会在其中自然布局。