I wonder if it is possible to have a flex grid with flex auto on the cells instead of flex 1 but that the cells of the neighboring rows still align. I created a small demonstration of the problem. When you click then button you switch from flex: 1
to flex: auto
on call cells.
const styles = document.documentElement.style;
const flexMode = document.querySelector('span')
let flexAuto = true;
function flexToggle(e) {
let flex = flexAuto ? 'auto' : '1'
styles.setProperty("--flex-mode", flex);
flexAuto = !flexAuto
flexMode.textContent = flex
}
:root{
--flex-mode: 1
}
html, body {
width: 100%;
}
table {
display: flex;
flex-direction: column;
margin: auto;
width: 50%;
min-height: 0;
max-height: 8rem;
border: dashed thin;
}
thead, tbody {
display: flex;
flex-direction: column;
}
tbody {
flex: 1;
overflow: auto;
}
tr {
display: flex;
padding: .3rem;
border-bottom: blue solid;
}
th, td {
flex: var(--flex-mode);
text-align: left;
border: solid thin;
padding: 0.2rem 0.5rem;
}
thead tr {
padding-right: 21px;
}
<table>
<thead>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
</tr>
</thead>
<tbody>
<tr>
<th>short</th>
<th>long in this cell</th>
<th>medium</th>
</tr>
<tr>
<th>lil short</th>
<th>long content in this cell</th>
<th>medium sized</th>
</tr>
<tr>
<th>almost short</th>
<th>very long content in this cell</th>
<th>medium sized stuff</th>
</tr>
</tbody>
</table>
<p>
<button onclick="flexToggle(event)">Change Flex Mode</button>
Flex Mode: <span>1</span>
</p>