skip to Main Content

I am creating a bootstrap 5 page, but facing a challenge to stretch two child DIVs to the same height as the parent. Can someone help me please?

Here is the HTML:
https://jsfiddle.net/og7pz9sq

In the above fiddle, I am trying to make the light-green and light-yellow DIVs to occupy the while white area. Footer comes just after this white area, and that will always remains at the bottom of the page.

<!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/[email protected]/dist/css/bootstrap.min.css"
          rel="stylesheet"
          integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
          crossorigin="anonymous">
</head>

<body>
    <div id="wholepage"
         style="min-height: 90dvh;">

        <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">

            <div class="row">

                <div id="sidebar"
                     class="col-md-auto"
                     style="display: inline;">

                    <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="mt-5 p-4 bg-dark text-white text-center">
        <p>Footer</p>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
            integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
            crossorigin="anonymous"></script>
</body>

</html>

2

Answers


  1. The solution is to push away from #wholepage and use flexbox to stretch the descendants.
    Somehow:

    .row {
      background: #f8f9fa;
      margin-top: 20px;
    }
    
    .col {
      border: solid 1px #6c757d;
      padding: 10px;
    }
    
    /* new */
    
    #wholepage {
      display: flex;
      flex-direction: column;
    }
    
    #sidebar-and-contents {
      display: flex;
      flex-direction: column;
      flex: auto;
    }
    
    #sidebar-and-contents .row {
      flex: auto;
    }
    
    #sidebar {
      display: flex!important;
      flex-direction: column;
    }
    
    #sidebar .nav {
      flex:auto;
    }
    <!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/[email protected]/dist/css/bootstrap.min.css"
              rel="stylesheet"
              integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
              crossorigin="anonymous">
    </head>
    
    <body>
        <div id="wholepage"
             style="min-height: 90dvh;">
    
            <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">
    
                <div class="row">
    
                    <div id="sidebar"
                         class="col-md-auto"
                         style="display: inline;">
    
                        <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="mt-5 p-4 bg-dark text-white text-center">
            <p>Footer</p>
        </div>
    
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
                integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
                crossorigin="anonymous"></script>
    </body>
    
    </html>
    Login or Signup to reply.
  2. You can consider using a vertical flex box to fix the footer to the end of the page and make the middle section grow to fill the spaces. d-flex flex-column is added to the two parent container (wholepage and sidebar-and-contents).

    Since you are using a media query break point md for a different layout, consider adding flex-column flex-md-row in the sidebar-and-contents container to make sure the content container grows in the correct direction for smaller screen sizes.

    See below example: (I’ve excluded the extra padding and margins for clearer show case.)

    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    
    <div id="wholepage" style="min-height: 90dvh;" class="d-flex flex-column">
      <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="d-flex flex-column col">
        <div class="row col me-0 flex-column flex-md-row">
          <div id="sidebar" class="col-md-auto pe-0" style="display: inline;">
            <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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search