我正在尝试使用 flexbox 制作以下布局。
顶部黑色条、中间部分和底部黑色条都是弹性项目,它们都是主体标签内的包装 div 的子元素。该标签如下所示。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic' rel='stylesheet' type='text/css'>
<link href="~/Styles/bootstrap.min.css" rel="stylesheet" />
<link href="~/Styles/font-awesome.min.css" rel="stylesheet" />
<link href="~/Styles/ppt_site.css" rel="stylesheet" />
</head>
<body>
<div class="wrapper">
<div id="topRow">
</div>
<div id="centralRow">
<div id="sidebarColumn">
</div>
<div id="contentColumn">
</div>
</div>
<div id="bottomRow">
</div>
</div>
@Section['customScripts']
<script src="~/Scripts/siteGlobals.js"></script>
<script src="~/requireconfig"></script>
<script data-main="~/scripts/NewTemplatesAppLoader.js" src="~/requirejs"></script>
</body>
</html>
控制所有这些的样式表如下:
.wrapper, html, body {
height: 100%;
margin: 0;
}
.wrapper {
display: flex;
-ms-flex-direction: column;
-webkit-flex-direction: column;
flex-direction: column;
}
#topRow {
background-color: black;
color: white;
}
#centralRow {
-ms-flex: 1;
-webkit-flex: 1;
flex: 1;
display: flex;
-ms-flex-direction: row;
-webkit-flex-direction: row;
flex-direction: row;
}
#bottomRow {
background-color: #333333;
color: white;
}
#sidebarColumn {
background-color: #B83B1D;
color: white;
}
#contentColumn {
-ms-flex: 1;
-webkit-flex: 1;
flex: 1;
background-color: #F7F7F7;
color: black;
padding: 10px;
overflow-y: auto;
}
就主要布局而言,一切都很完美,但是如果您查看图片,您会注意到红色侧边栏顶部有一个黄色边框的区域。
那个区域注定是一个div,它将延伸到上面CSS中红色区域(侧边栏列)的整个高度,但无论我如何尝试,都无法使其达到预期效果。
理想情况下,我希望它是一个flex容器的div,然后我可以将4个flex项放入其中,并让每个项目占用可用空间的四分之一,但由于具有黄色边框的div似乎无法看到父容器的高度(红色侧边栏本身也是flex项),因此它只占据其内容周围的空间高度。
我尝试了将其包装在更多的div中并定位这些div,我将进一步的flex盒子包裹在更多的div中,我尝试了块、行内、表格单元格和几乎我能想到的所有其他东西。
无论我尝试什么,我似乎都无法使黄色边框的容器与其父容器具有相同的高度。
大家有什么想法吗?
更新(第二天)
这两种方案确实能够正常工作,它们本身就是我所期望的。
然而,在我的特定情况下,它们不起作用。
我正在使用 NancyFX 和其超级简单的视图引擎,并且我正在使用 KnockoutJS 来组合“Web 组件”,以便我可以保持我的部件可重用。
如果我编辑我的“主模板”,并将 div 直接插入标记中,嵌套在 #sidebarColumn 下面,我会得到所需的 4 个相等大小的 div。
然而,实际上需要发生的是,我的模板如下所示:
<div class="wrapper">
<div id="topRow">
@Section['topbarContent']
</div>
<div id="centralRow">
<div id="sidebarColumn">
@Section['sidebarContent']
</div>
<div id="contentColumn">
@Section['bodyContent']
</div>
</div>
<div id="bottomRow">
@Section['bottombarContent']
</div>
</div>
如果我移除"@Section['sidebarContent']",并直接放置div,则它们可以正常工作。
如果我将div直接放入使用以下模板的"Page"中:
@Master['shared/ppt_layout.html']
@Section['topbarContent']
<toptoolbar></toptoolbar>
@EndSection
@Section['sidebarContent']
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
@EndSection
@Section['bodyContent']
<h1>Body Content</h1>
<p class="lead">I am the test page that uses a wide sidebar component</p>
<p>If you notice however, I also have a top toolbar with title added to me, and a footer bar.</p>
<p>We all use the same template however....</p>
@EndSection
@Section['bottombarContent']
<footerbar></footerbar>
@EndSection
@Section['customScripts']
@EndSection
那也可以,但如果我把标记放在一个knockout组件中,即使只是像页面上一样的普通div,那么我就会得到我最初注意到的效果。因此,由于某种原因,使用knockout组件添加内容正是导致问题的根源。即使我尝试在组件内构建新的flex布局,垂直方向上什么都不起作用,但水平方向上一切都完美运行。例如,页脚栏被分成左右两个部分,没有任何问题,但垂直对齐却被压缩了。