You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让子DIV撑满父DIV高度?Bootstrap 5页面布局求助

解决Bootstrap 5布局问题:侧边栏/内容区撑满高度 + 底部固定Footer

关键改动说明

  • 利用Bootstrap的flex布局体系,让页面整体实现"头部-内容区-底部"的自适应结构,保证Footer固定在页面底部
  • 让侧边栏和内容区所在的容器及行元素继承父级高度,确保两列高度一致并填满剩余空间
  • 调整侧边栏导航列表的高度,让背景色覆盖整个侧边栏区域

修改后的完整代码

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1">

    <title>Bootstrap</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
          rel="stylesheet"
          integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
          crossorigin="anonymous">
    <style>
        #sidebar ul {
            height: 100%;
        }
    </style>
</head>

<body class="d-flex flex-column min-vh-100 m-0">
    <div id="wholepage" class="flex-grow-1">

        <div id="header">
            <div class="p-3 bg-primary text-white text-center">
                Bootstrap 5 Page
            </div>
        </div>

        <div id="topmenu">
            <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
                <div class="container-fluid">
                    <ul class="navbar-nav">
                        <li class="nav-item">
                            <a class="nav-link active"
                               href="#">Active</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link"
                               href="#">Link</a>
                        </li>
                    </ul>
                </div>
            </nav>
        </div>

        <div id="sidebar-and-contents" class="h-100">
            <div class="row h-100 align-items-stretch">
                <div id="sidebar" class="col-md-auto">
                    <ul class="nav nav-pills flex-column" style="background-color: rgb(172, 201, 135);">
                        <li class="nav-item">
                            <a class="nav-link"
                               href="#">Link-Left1</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link"
                               href="#">Link-Left2</a>
                        </li>
                    </ul>
                </div>

                <div id="content" class="col" style="background-color: rgb(241, 240, 172);">
                    <div>Main area for dynamic contents based on menu selections</div>
                </div>
            </div>
        </div>
    </div>

    <div id="footer" class="p-4 bg-dark text-white text-center">
        <p>Footer</p>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
            integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
            crossorigin="anonymous"></script>
</body>
</html>

核心改动点解析

  1. 页面整体结构优化:给<body>添加d-flex flex-column min-vh-100 m-0,将body设置为垂直方向的flex容器,高度占满整个视口;#wholepage添加flex-grow-1,让它自动填充头部和底部之外的剩余空间
  2. 内容区高度继承:给#sidebar-and-contents和内部的row都加上h-100,让它们继承父容器的高度;row添加align-items-stretch,确保左右两列高度完全一致
  3. 侧边栏背景覆盖:通过自定义CSS设置#sidebar ul { height: 100%; },让侧边栏的导航列表撑满所在列的高度,浅绿背景色覆盖整个侧边栏区域
  4. Footer固定调整:移除原mt-5类,避免不必要的间距,让Footer自然固定在页面底部

内容的提问来源于stack exchange,提问作者ssh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 17:51:08