html - 带有固定宽度左右侧边栏的 flex 内部网格
问题描述
我正在尝试使用 html 和 css 进行以下操作: 图像草图
基本上,我想做到这一点:
- 左侧和右侧边栏始终具有 200px 宽度,并且它们本身没有填充或边距(它们具有 100% 的高度)
- 中间有 12 列的网格 div 和 10px 的网格间隙
网格本身的结构并不重要(我知道这部分),但我无法以正确的顺序对齐 div(左侧、网格和右侧边栏)。
#container{
display: flex;
}
#left-sidebar{
background-color: blue;
height: 100%;
width: 200px;
position: fixed;
left: 0;
}
#grid{
display: grid;
grid-template-columns: repeat(12,1fr);
grid-template-rows: repeat(12, 1fr);
grid-gap: 10px;
height: 100vh;
padding: 10px;
flex: 1;
}
#right-sidebar{
background-color: blue;
height: 100%;
width: 200px;
position: fixed;
right: 0;
}
#g1{
background-color: orange;
grid-column: 1 / 4;
grid-row: 1 / last-line;
border-radius: 4px;
}
#g2{
background-color: red;
grid-column: 4 / 13;
grid-row: 1 / last-line;
border-radius: 4px;
}
<div id="container">
<div id="left-sidebar"></div>
<div id="grid">
<div id="g1"></div>
<div id="g2"></div>
</div>
<div id="right-sidebar"></div>
</div>
我希望网格位于这两个侧边栏之间...
解决方案
您可以在网格的左侧和右侧添加一个 200 像素的填充,因此它位于侧边栏之间。
#container{
display: flex;
}
#left-sidebar{
background-color: blue;
height: 100%;
width: 200px;
position: fixed;
left: 0;
}
#grid{
padding-left: 200px !important;
padding-right: 200px !important;
display: grid;
grid-template-columns: repeat(12,1fr);
grid-template-rows: repeat(12, 1fr);
grid-gap: 10px;
height: 100vh;
padding: 10px;
flex: 1;
}
#right-sidebar{
background-color: blue;
height: 100%;
width: 200px;
position: fixed;
right: 0;
}
#g1{
background-color: orange;
grid-column: 1 / 4;
grid-row: 1 / last-line;
border-radius: 4px;
}
#g2{
background-color: red;
grid-column: 4 / 13;
grid-row: 1 / last-line;
border-radius: 4px;
}
<div id="container">
<div id="left-sidebar"></div>
<div id="grid">
<div id="g1"></div>
<div id="g2"></div>
</div>
<div id="right-sidebar"></div>
</div>
推荐阅读
- flutter - Flutter Provider 嵌套对象
- css - 子菜单使用与顶部菜单相同的颜色
- asp.net-core - 为什么 CORS 在 ASP.NET Core 3 Web Api 中不起作用?
- swift - 函数不允许返回值
- python - 有没有一种简单的方法可以在 Python 中向 ISBN 添加破折号?
- html - 带有图像的 CSS 形状遮罩
- c# - .NET 函数中的线程永远不会返回
- android - Firebase 应用内消息 - Android 中的 PERMISSION_DENIED(请求被阻止问题)
- c# - 如何在 ABP 中使用 XUnit Collection Fixtures?
- java - 单击按钮后如何“SPLASH_TIME”停止?