我有一个带有页脚的移动侧边栏,该页脚应该固定在底部,但是当有很多导航链接时,页脚会被向下推,您必须滚动才能看到它。
这是一个简化的演示,显示侧边栏已经在内容上展开。您可以看到页脚粘在侧边栏的底部,没有被额外的链接向下推,并且在滚动时它仍然与链接重叠。
body {background: #CCC;}
#side-menu {
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 200px;
background: #FFF;
box-shadow: 0 0 20px #000;
display: flex;
flex-direction: column;
overflow: auto;
height: 100vh;
}
#side-menu-header,
#side-menu-footer {
flex-shrink: 0;
background-color: skyblue;
padding: 0.5rem;
}
#side-menu-nav {
flex-grow: 1;
display: flex;
flex-direction: column;
}
#side-menu-nav a {
padding: 0.5rem;
}
<aside id="side-menu">
<header id="side-menu-header">
Logo
</header>
<nav id="side-menu-nav">
<a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a>
</nav>
<footer id="side-menu-footer">
Footer
</footer>
</aside>
<p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p>
我过去曾使用过 flexbox,但我不知道我做错了什么。如何使页脚显示在导航链接下方?
此外,您的#side-menu-nav
需要有flex-shrink: 0;
,否则它会收缩以适合 它的 parent 。
堆栈片段
body {background: #CCC;}
#side-menu {
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 200px;
background: #FFF;
box-shadow: 0 0 20px #000;
display: flex;
flex-direction: column;
overflow: auto;
height: 100vh;
}
#side-menu-header,
#side-menu-footer {
flex-shrink: 0;
background-color: skyblue;
padding: 0.5rem;
}
#side-menu-nav {
flex-shrink: 0;
flex-grow: 1;
display: flex;
flex-direction: column;
}
#side-menu-nav a {
padding: 0.5rem;
}
<aside id="side-menu">
<header id="side-menu-header">
Logo
</header>
<nav id="side-menu-nav">
<a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a><a href="#">link</a>
</nav>
<footer id="side-menu-footer">
Footer
</footer>
</aside>
<p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p><p>lots of content here oh yeah so much content!!!</p>
我是一名优秀的程序员,十分优秀!