Steps for Adjusting Column Heights in Bootstrap 4

Currently, I am experimenting with Bootstrap 4 and facing some challenges in setting the correct heights for my layout. The elements in my design include a "side nav", "header", "main", and "footer".

My goal is to make the "main" section occupy most of the height on the page. However, due to an issue with the heights, the side nav does not extend all the way down and there is a white space in the bottom left corner.

If you view my code in full screen mode, you will notice this issue.

body,
html,
.container-fluid {
  height: 100%;
}

.wrapper {
  display: flex;
  align-items: stretch;
}

#sidebar {
  background-color: blue;
  color: white;
}

@media (max-width: 768px) {
  #sidebar {
    min-width: 80px;
    max-width: 80px;
    text-align: center;
    margin-left: -80px !important;
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">

  <title>test</title>

  <!-- Bootstrap CSS CDN -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
  <!-- Our Custom CSS -->
  <link rel="stylesheet" href="style4.css">
</head>

<body>
  <div class="wrapper h-100">
    <nav id="sidebar">
      <div class="sidebar-header">
        <h3>Bootstrap Sidebar</h3>
      </div>

      <ul class="list-unstyled components">
        <li>
          <a>
                        Home
                    </a>
        </li>
      </ul>
    </nav>

    <div class="container-fluid ">
      <div class="row h-100">
        <div class="col-12" style="background-color: red;">
          One of three columns
        </div>
        <main class="col-12 h-100" style="background-color: yellow;">
          test
        </main>
        <div class="col-12" style="background-color: pink;">
          test2
        </div>
      </div>
    </div>
  </div>
</body>

</html>

Answer №1

An issue arises when using the h-100 class on the parent row, as it causes the child col-12 to also be set to h-100, extending beyond the viewport height and leading to scrolling and extra whitespace below the sidebar.

Thankfully, starting from Bootstrap 4.1, a new utility class called flex-fill has been introduced to address this layout challenge...

.flex-fill {
  flex: 1 1 auto;
}

To resolve this issue, you can apply the flex-fill class to ensure that the child divs expand to fill the remaining height. Simply add a min-height property to the .wrapper element:

.wrapper {
    min-height: 100vh;
}

Furthermore, convert the container-fluid into a flexbox container by adding the d-flex class. Then utilize the flex-fill class to make both the row and the middle col-12 occupy the full height:

<div class="wrapper d-flex">
    <!-- Sidebar Holder -->
    <nav id="sidebar">
        <div class="sidebar-header">
            <h3>Bootstrap Sidebar</h3>
        </div>
        <ul class="list-unstyled components">
            <li>
                <a>
                    Home
                </a>
            </li>
        </ul>
    </nav>
    <div class="container-fluid d-flex flex-column">
        <div class="row flex-fill flex-column">
            <div class="col-12" style="background-color: red;">
                One of three columns
            </div>
            <main class="col-12 flex-fill" style="background-color: yellow;">
                test
            </main>
            <div class="col-12" style="background-color: pink;">
                test2
            </div>
        </div>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method to transfer this logo to my header without affecting the title's placement?

I'm currently working on a website and I am trying to figure out how to position the title and logo in the header. Ideally, I want the title to be centered and the logo to be on the left. However, I've encountered an issue where the title is cau ...

Searching for a particular Prettier setting

Seeking a Nicer alternative. I am exploring if there is a way to format code like this without moving the first attribute to a new line: <div class="test-class" [test-binding]="true" (test-binging)="test()" ...

Rod running from one end to another within a receptacle

I'm facing a challenge with my container (CardContainer) and its child elements. I want to create a bar that fills the space between the card image and the chevron, but I'm having trouble figuring out the best approach. How can I ensure that this ...

The CSS :not selector does not work as intended

My goal is to assign the class no-color to a specific tag. When JavaScript is enabled, I want this class to be removed and the text to be colorized. However, my current CSS solution is not working as expected. Below is a sample code snippet demonstrating t ...

Can the active link color be modified in Bootstrap 5 without the use of CSS?

It's been a while since I've worked with HTML, and this is only my second attempt at creating tabs, so I might be approaching this the wrong way. Currently, the link text in the tabs is in the primary bootstrap color (blue) by default and change ...

"Is it possible to move the text on the canvas by dragging it to where you want it to be

Seeking help again after an unsuccessful attempt. How can I allow the user to add text to the canvas by dragging it to their desired location? For example, if they input text somewhere, it should appear on the canvas and then be draggable to any position ...

What is the process of making circle or square shapes with text inside using React, without the use of SVG images?

Is there a way to create circle and square shapes in React with custom text inside, without relying on SVG images? Check out this example: https://i.sstatic.net/iHZgy.png I attempted the code below but it did not render any shapes: import React from &apos ...

Elements listed are often ignored by HTML

My website sends a URL and xpath query to the server in order to extract data from the URL based on the xpath query. While it is functioning properly, I am encountering a singular issue. When I specify an xpath query for href,text, it displays a list of a ...

Place the button using the "fixed" position in a React Native application

Is there a way to place a button in the bottom right corner with a fixed position in React Native? The position: fixed property does not work in React Native, and the stickyHeaderIndices method in ScrollView does not allow positioning an element above oth ...

Is SCSS automatically compiled into CSS by VSCode?

In the tutorial I followed, the instructor mentioned clicking a button at the bottom of the window to create a CSS file with the 'Live Sass Compiler' extension in VSCode. However, despite following the instructions, the compilation does not seem ...

What could be causing my JavaScript calculator to malfunction?

I can't seem to figure out why my calculator is malfunctioning and I'm at a loss as to what the issue might be. I even searched on forums like stackoverflow but couldn't find a solution. It seems like there could be an error in the JavaScrip ...

When the play button is clicked, the video slides over to the left

Whenever I attempt to click on the video link, it seems to shift over to the left side of the page. The link is positioned in the center and I would prefer for the video to open up at the same central location. However, it consistently opens up on the left ...

I need assistance on combining two separate images within one div. Can someone provide guidance on how to achieve this?

Here is a way to place two images in one div, with one image at the top and the other at the bottom. Each image should take up approximately 25% of the width: <div class="images"> <div class="pics"> <img src="GRProvider/Img.jpg" ...

Manipulating video volume using JavaScript injection

My web page includes a video, and I have successfully used JavaScript injection to control the play/pause functionality. Now, I am looking to also adjust the volume based on percentage. How can I create a function that decreases or increases the volume acc ...

Using CSS for hover transitions can be glitchy in Safari, especially when trying to keep images centered

After seeing an example on Design Shack, I was inspired to create linkable photos that zoom in slightly when hovered over. To achieve the desired centered animation effect, I had to tweak the top, left, margin-top, and margin-left properties until it worke ...

The content at “http://localhost:3000/script.js” could not be accessed because of a MIME type mismatch with the X-Content-Type-Options set to nosniff

I encountered an issue while attempting to transfer all the JavaScript to a separate js file and then adding that js file to the html per usual. The console displayed the following error message: The resource from “http://localhost:3000/public/main.js” ...

Placing elements in an exact position within a parent container is causing an overlap with the

I am struggling to grasp why a div set as absolute inside a relative div is overlapping with other elements. From what I understand, it should remain fixed within its parent relative div, right? This is my solution: #container { position: relative; ...

Implement various actions when the window is resized

The carousel code I have found returns a different number of items to display based on screen size. You can check it out here. $(function() { var mini = 1000; var big = 1280; if (window.screen.availWidth < mini) { $('#carousel1').jsCar ...

Showcasing JSON data in an HTML table using Angular

I'm facing an issue with displaying a JSON response in a table. Below is an excerpt from my controller.js file: var app = angular.module('angularjs-starter', []); app.controller('MainCtrl', function($scope, $http) { $scope.choice ...

Rotating an image as it approaches the footer: a step-by-step guide

I'm trying to add a navigation arrow on my website that will rotate to point to the top when it reaches the footer. When clicked on, it should scroll back up to the top. The arrow already has a scrolling effect, but I need help figuring out how to m ...