如何使固定高度容器内的侧边栏与内容区垂直填充空间?
只需调整两处CSS设置即可实现需求:
- 修改
.projects-content的align-items属性,将flex-start改为stretch,让子元素自动拉伸填充父容器垂直空间 - (可选)若需sidebar内部元素垂直分布,可给
.sidebar添加justify-content属性(比如space-between),充分利用空间
修改后的完整代码
CSS
* { font-family: "Poppins", sans-serif; } body { background-size: 100%; /*background-image: url('./images/diego-ph-5LOhydOtTKU-unsplash.jpg');*/ background-color: black; background-repeat: "no-repeat"; background-position: 0 0; color: aliceblue; margin: 0; padding: 0; } .projects-table { border-style: solid; border-radius: 1rem; border-width: 0.25rem; border-color: slategrey; margin-top: 5rem; padding: 1rem; height: 20rem; display: flex; flex-direction: column; } .projects-table .search { opacity: 0.25; color: white; display: flex; flex-direction: row; justify-content: start; align-items: center; border-bottom: solid; border-width: 0.1rem; } .projects-table .search img { width: 1.25rem; height: 1.25rem; margin-right: 0.25rem; } .projects-table .projects-content { display: flex; flex-direction: row; justify-content: space-between; /* 修改此处:将flex-start改为stretch */ align-items: stretch; margin-top: 1rem; flex-grow: 1; border-style: solid; } .projects-table .projects-content .sidebar { width: 10%; height: 100%; display: flex; flex-direction: column; align-items: center; /* 可选:添加justify-content让内部元素垂直分布 */ justify-content: space-between; border-style: solid; margin: 0.5rem; } .projects-table .projects-content .sidebar p { border-style: solid; border-width: 0.1rem; border-radius: 0.5rem; margin-top: 0; margin-bottom: 0.5rem; padding: 1rem; } .projects-table .projects-content .content { width: 100%; height: 100%; margin: 0.5rem 1rem 0.5rem 1rem; border-style: solid; border-width: 0.1rem; border-radius: 0.5rem; padding: 1rem; }
HTML
<div class="projects-table"> <div class="search"> <h3>Search...</h3> </div> <div class="projects-content"> <div class="sidebar"> <p>Sidebar</p> <p>Sidebar</p> </div> <p class="content">content</p> </div> </div>
内容的提问来源于stack exchange,提问作者Anya
相关产品推荐
相关产品推荐

