我有一个可变大小的侧边栏菜单。它有随机数量的菜单标题和项目。在此菜单之后,<div>
应覆盖剩余的高度。
我尝试了this解决方案并且它会起作用,但<div class="menucell">
元素上有一个填充(小提琴中的灰色空格)。
如何删除此填充?或者是否有更好的解决方案来完成这项任务?
.menurow {
display: table-row;
vertical-align: top;
box-sizing: border-box;
}
.menucell {
display: table-cell;
padding: 0;
vertical-align: top;
}
.menu {
position: fixed;
display: table;
box-sizing: border-box;
width: 300px;
height: calc(100vh - 20px);
background-color: rgba(0, 0, 0, 0.1);
}
.menuSubheader {
height: 15px;
padding: 8px 5px 8px 15px;
font-size: 12px;
line-height: 15px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
position: relative;
background-color: rgba(0, 0, 255, 0.3);
}
.itemList {
width: 300px;
height: auto;
z-index: 80;
background-color: rgba(0, 0, 255, 0.1);
}
.item {
height: 15px;
padding: 5px 5px 5px 15px;
background-color: rgba(255, 255, 255, 0.8);
}
.buffer {
width: 300px;
margin-top: 10px;
overflow: hidden;
text-align: center;
display: table-cell;
align-content: center;
vertical-align: middle;
color: rgba(211, 47, 47, 0.5);
background-color: rgba(211, 47, 47, 0.1);
border: 2px dashed rgba(211, 47, 47, 0.3);
}
<div class="menu">
<div class="menurow">
<div class="menucell">
<div class="menuSubheader"><span>Header1</span></div>
<div class="itemList">
<div class="item"><span>Item</span></div>
<div class="item"><span>Item</span></div>
<div class="item"><span>Item</span></div>
</div>
<div class="menuSubheader"><span>Header2</span></div>
<div class="itemList">
<div class="item"><span>Item</span></div>
<div class="item">
<span>Item</span>
</div>
</div>
<div class="menuSubheader" data-visible="false"><span>Header3</span></div>
<div class="itemList">
<div class="item"><span>Item</span></div>
<div class="item"><span>Item</span></div>
<div class="item"><span>Item</span></div>
</div>
</div>
</div>
<div class="buffer">
<span>please fill parent height</span>
</div>
</div>
答案 0 :(得分:3)
Flex确实是最简单的方法。
sin $ pi*x
flex-grow:1;
或.menu {
min-height: 100vh;/* or height */
display: flex;
flex-direction: column;
}
.buffer {
flex: 1;
}
简写是您需要应用于.buffer;)
这定义了Flex项目在必要时增长的能力。它接受一个无比的值作为一个比例。它规定了项目应占用的Flex容器内可用空间的大小。
下面的演示
flex:1
&#13;
.menurow {}
.menucell {}
.menu {
width: 300px;
padding: 5px;
background-color: rgba(0, 0, 0, 0.1);
min-height: 100vh;/* here ============= or height */
display: flex;/* here ============= */
flex-direction: column;/* here =================*/
box-sizing: border-box;
}
.menuSubheader {
height: 30px;
padding: 8px 5px 8px 15px;
font-size: 18px;
line-height: 30px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
position: relative;
background-color: rgba(0, 0, 255, 0.3);
}
.itemList {
width: 300px;
background-color: rgba(0, 0, 255, 0.1);
}
.item {
height: 35px;
padding: 5px 5px 5px 15px;
background-color: rgba(255, 255, 255, 0.8);
}
.buffer {
flex: 1;/* here ====================== */
color: rgba(211, 47, 47, 0.5);
background-color: rgba(211, 47, 47, 0.1);
border: 2px dashed rgba(211, 47, 47, 0.3);
}
/* demo purpose */
.flex-center {
display: flex;
flex-flow: column;
align-items: center;
justify-content: center;
}
&#13;
如果您仍想使用表<div class="menu">
<div class="menurow">
<div class="menucell">
<div class="menuSubheader"><span>Header1</span></div>
<div class="itemList">
<div class="item"><span>Item</span></div>
<div class="item"><span>Item</span></div>
<div class="item"><span>Item</span></div>
</div>
</div>
</div>
<div>And so on ....</div>
<div class="buffer flex-center">
<span>please fill parent height</span>
</div>
</div>
,则需要先编写一些CSS,您必须要注意容器及其直接子项:
display
下面的演示
.menu {
display: table;
table-layout: fixed; /* to avoid expanding over width set */
height: 100vh; /* acts like min-height */
}
.menurow {
display: table-row;
height: 1%;/* it will expand to fit content */
}
.menucell {
display: table-cell;
}
.buffer {
height: 100%;
}
&#13;
.menu {
display: table;
table-layout: fixed; /* to avoid expanding over width set */
height: 100vh; /* acts like min-height */
}
.menurow {
display: table-row;
height: 1%;/* it will expand to fit content */
}
.menucell {
display: table-cell;
}
.buffer {
height: 100%;/* will expand within space left */
}
.menu {
width: 300px;
padding: 5px;
background-color: rgba(0, 0, 0, 0.1);
box-sizing: border-box;
}
.menuSubheader {
height: 30px;
padding: 8px 5px 8px 15px;
font-size: 18px;
line-height: 30px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
position: relative;
background-color: rgba(0, 0, 255, 0.3);
}
.itemList {
width: 300px;
background-color: rgba(0, 0, 255, 0.1);
}
.item {
height: 35px;
padding: 5px 5px 5px 15px;
background-color: rgba(255, 255, 255, 0.8);
}
.buffer {
color: rgba(211, 47, 47, 0.5);
background-color: rgba(211, 47, 47, 0.1);
border: 2px dashed rgba(211, 47, 47, 0.3);
}
/* demo purpose */
.cell-center {
display: table-cell;
vertical-align: middle;
text-align: center;
}
body {
margin:0;
}
&#13;
以整页模式播放片段以测试行为
答案 1 :(得分:1)
试试flexboxes :) 定位真的比桌子和旧的黑客更酷:
https://jsfiddle.net/5L9331m1/1/
我做了什么:
.menu
display
切换为flex
.menu
flex-direction
添加到column
.buffer
height: 100%
无论如何,请查看:https://css-tricks.com/snippets/css/a-guide-to-flexbox/
希望这有帮助!
PS:我刚用垂直填充修复了问题,但你当然可以使用flexbox来重构你的整个菜单;)