html - 在同级弹性行中对齐弹性自动单元格
问题描述
我想知道是否可以在单元格上使用 flex auto 而不是 flex 1 的 flex 网格,但相邻行的单元格仍然对齐。我创建了这个问题的一个小演示。当您单击 then 按钮时,您从呼叫单元切换flex: 1
到flex: auto
呼叫单元。
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>
解决方案
您需要为这些单元格设置宽度
th, td {
width: calc(100% / 3);
}
因为flex:auto
会根据内容调整宽度。
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 {
width: calc(100% / 3);
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>
推荐阅读
- javascript - Googlemaps Api 奇怪的行为
- javascript - 如何从 Firestore 获取集合数据
- amazon-web-services - EKS:验证 ec2 和 rds 实例之间的连接以及 nslookup 和 telnet 之间的区别
- android - 使用 Intent.ACTION_OPEN_DOCUMENT 选择文件并使用 Retrofit2 上传到服务器
- c# - 如何比较两个列表,如果它们具有相等的值,则取列表的两个索引
- perl - 两个日期之间的天/月/年
- docker - Webserver 无法通过 docker-compose 连接到 Redis
- jquery - JQuery 包含 vs 等于
- plsql - 如何从 PL/SQL 中的 blob 变量中提取子字符串?
- react-native - 在 react native 中使用 WonderPush