2025-03-11 html+css 子div如何占满父div剩余高度 以下代码解决的问题: 子容器占满父容器剩余高度 子容器占满父容器剩余高度时,另其兄弟容器高度保持不变 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778<html lang="en"><head> <title>Flex布局,为什么items加载完毕后,height会比设置的值要小</title> <style> body, html { margin: 0; padding: 0; height: 100%; } .container { display: flex; flex-direction: column; height: 100vh; /* 父容器占满整个视口高度 */ } .header { flex-shrink: 0; height:500px; /* 如果不加flex-shrink: 0;那么items加载完毕后,height会变小*/ padding: 10px; background-color: #f0f0f0; border-bottom: 1px solid #ccc; } .content { flex-grow: 1; /* 占满父容器剩余高度 */ overflow-y: auto; /* 当内容超出时,显示垂直滚动条 */ padding: 10px; } .item { padding: 10px; height:200px; border-bottom: 1px solid #eee; } </style></head><body> <div id="app"> <div class="container"> <div class="header"> <h2>高度是500PX</h2> </div> <div class="content"> <div v-for="item in items" :key="item.id" class="item"> {{ item.text }} </div> </div> </div> </div> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> <script> const { createApp } = Vue; createApp({ data() { return { items: [] // 初始为空数组 }; }, mounted() { // 模拟延迟加载数据 setTimeout(() => { this.items = [ { id: 1, text: 'Item 1' }, { id: 2, text: 'Item 2' }, { id: 3, text: 'Item 3' }, { id: 4, text: 'Item 4' }, { id: 5, text: 'Item 5' }, { id: 6, text: 'Item 6' }, { id: 7, text: 'Item 7' }, { id: 8, text: 'Item 8' }, { id: 9, text: 'Item 9' }, { id: 10, text: 'Item 10' } ]; }, 2000); // 延迟 2 秒渲染 } }).mount('#app'); </script></body></html> 前一篇 多层嵌套的div且高度都不固定,让子div内容滚动 后一篇 el-input 各种输入限制的正则整理