html - 如何在带有伸缩功能的 flexbox 盒子的每一侧放置 4 个“托盘”?
问题描述
有一个主 div (#root),我需要 4 个内部 div,每个 div 都在一侧完全拉伸(运行代码片段以查看)。
现在我在这里:
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
text-align: center;
}
#root {
background-color: blue;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
height: 100%;
}
.tray {
box-sizing: border-box;
background-color: red;
border: thin solid black;
}
.tray-top {
height: 48px;
width: 100%;
}
.tray-bottom {
height: 48px;
width: 100%;
align-self: flex-end;
}
.tray-left {
width: 48px;
}
.tray-right {
width: 48px;
}
<div id="root">
<div class="tray tray-top">top</div>
<div class="tray tray-left">left</div>
<div class="tray tray-right">right</div>
<div class="tray tray-bottom">bottom</div>
</div>
现在我想在left
和之间right
完全伸展。top
bottom
请注意,所有托盘都有固定宽度(左、右)或固定高度(顶部、底部)。
我会避免将更多的 div 嵌套到现有的中。
Flexbox 不是必须的,但与其他可能性相比,我发现它很容易且面向未来。
解决方案
CSS-Grid 可以做到这一点:
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
text-align: center;
}
#root {
background-color: blue;
display: grid;
grid-template-columns: auto 1fr auto;
grid-template-rows: auto 1fr auto;
height: 100%;
}
.tray {
box-sizing: border-box;
background-color: red;
border: thin solid black;
}
.tray-top {
height: 48px;
grid-column: 1 / -1;
}
.tray-bottom {
height: 48px;
grid-column: 1 / -1;
}
.tray-left {
width: 48px;
}
.tray-right {
width: 48px;
grid-column:3;
}
<div id="root">
<div class="tray tray-top">top</div>
<div class="tray tray-left">left</div>
<div class="tray tray-right">right</div>
<div class="tray tray-bottom">bottom</div>
</div>
推荐阅读
- python - Select column dynamically in Pandas dataframe based on values in a list or another column
- python - 如何使用pyqt5在python中制作每周时间表?
- javascript - fetch 在 await 之后仍然返回 Promise
- reactjs - 获取数组的最后一个元素,但在按日期排序之前
- ffmpeg - MOOV atom not found - 有没有办法从损坏的 mp4 中提取音频?
- ubuntu - Ubuntu 桌面应用程序会在 Ubuntu Touch OS 和使用 Electron 的设备上运行吗?
- monitoring - 有效监控?如何避免交替疲劳?
- python - 如何使用 python 在 dialogflow CX 中传递查询参数
- c# - RadGridView 子行中的用户控件 - Telerik Winforms
- python-3.x - 是否可以使用 api 将数据框保存到 atlassian 页面?