Ways to provide exclusive height control to a single Bootstrap column

I am currently working on a page layout that consists of two columns - one for navigation and the other for content.

.bg-1{
  background-color:orange;
  height: 400px;
  overflow-y: auto;


}
.bg-2{
  background-color: red;
  height: 200px

}
.row{
  align-items:flex-start;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class='container'>

  <div class='row'>
    <div class='col-3 bg-1'>
    Nav<br/>
    Hello<br/>
    Hello<br/>
    Hello<br/>
    I need a scrollbar!<br/>
    And only be as tall as my red neighbor!
    
    
    </div>
    <div class='col-9 bg-2'>Content<br/>I can be as tall as I want!</div>
  
  
  </div>




</div>

Currently, I don't mind if the content column (red) increases in height and takes up more space. However, I would like the navigation column to dynamically adjust its height based on the content column's height.

Sometimes, the navigation column also becomes quite long, resulting in users having to scroll past it to see the bottom of the content. I would prefer if the navigation column could adapt to the height of the content column and use a scrollbar for any overflow.

How should I configure my CSS so that the content column can expand as much as necessary within the flex container while the navigation column is limited to the height of the content column and uses a scrollbar for overflow?

I have tried:

  • Setting a maximum height for the parent container, but it does not limit the navigation column's height

  • Setting a maximum height for the navigation column, but I cannot find a value that matches the content column's height

  • Using align-items: flex-start, which prevents the content column from growing, but users still have to scroll past it to reach the bottom of the navigation. I want the focus to be on the content, so the navigation should utilize a scrollbar when it exceeds the content's height.

Thank you!

Answer №1

To achieve this, you can place another div within the left column and style it with position:absolute...

Demo: https://codeply.com/p/TvjnlB0BTj

.bg-1{
  background-color:orange;
}

.bg-2{
  background-color: red;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class='container'>
    <div class='row'>
        <div class='col-3 bg-1 overflow-auto'>
            <div class="position-absolute">
            Nav<br /> 
            Hello<br /> 
            Hello<br /> 
            Hello<br /> 
            Hello<br /> 
            Hello<br /> 
            Hello<br /> 
            Hello<br /> 
            Hello<br /> 
            Hello<br /> 
            I need a scrollbar!<br /> 
            And only be as tall as my red neighbor!
                        
            </div>
        </div>
        <div class='col-9 bg-2'>
            Content<br />I can be as tall as I want!
            <p>Shoreditch vegan artisan Helvetica. Tattooed Codeply Echo Park Godard kogi, next level irony ennui twee squid fap selvage. Meggings 
flannel Brooklyn literally small batch, mumblecore PBR try-hard kale chips. Brookch-key Odd Future. Austin messenger bag normcore, 
Helvetica Williamsburg sartorial tote bag distillery Portland before they sold out gastropub taxidermy Vice.</p>
            <p>Tattooed Codeply Echo Park Goda taxidermy Vice.</p>
        </div>
    </div>
</div>

https://codeply.com/p/TvjnlB0BTj

Answer №2

To ensure uniformity, make sure to set the same height for every div element.

Here's an example:

#div1{
  background-color:orange;
  height: 300px;
}

#div2{
  background-color: red;
  height: 300px;
}
.row{
  align-items:flex-start;
}
 
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class='container'>

  <div class='row'>
    <div id="div1" class='col-3'>
    Navigation<br/>
    Greetings<br/>
    Introductions<br/>
    Wishing you a scrollbar!<br/>
    And match the height of your neighboring red box!
    
    
    </div>
    <div id="div2" class='col-9'>Content<br/>I can stretch as much as I desire!</div>
  
  </div>;

</div>

Answer №3

I had to resort to using JavaScript because achieving this effect with CSS seemed impossible

function adjustDivHeight() {
  let originalHeight = $('.right').height();
  let currentDiv = $('.left');
  $(currentDiv).css('height', originalHeight);
}

window.onload = function() {
  adjustDivHeight();
};

$(window).resize(function() {
  adjustDivHeight();
});
.bg-1 {
  background-color: orange;
  overflow: auto;
}

.bg-2 {
  background-color: red;
  height: 200px
}

.row {
  align-items: flex-start;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<div class='container'>
  <div class='row'>
    <div class='col-3 bg-1 left'>
      Nav<br/> Hello
      <br/> Hello
      <br/> Hello
      <br/> I need a scrollbar!<br/> And only be as tall as my red neighbor!
    </div>
    <div class='col-9 bg-2 right'>Content<br/>I can be as tall as I want!</div>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>

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

Using Bootstrap 4 allows me to make the top navbar fixed when scrolling. Is there a way to also make another line below it fixed in normal CSS?

Is it possible to create a top-fixed navigation using the navbar in HTML? I also want to make another heading text that remains fixed even when the page is fully scrolled. I am having trouble achieving this effect. Please suggest if it can be achieved wi ...

How can I make a Bootstrap video embed fill the entire width of the screen?

I'm struggling to integrate a YouTube video into my webpage with Bootstrap, but I want it to span the entire width of the page. Here's the HTML code: <iframe class="embed-responsive-item youtube" src="https://www.youtube.com/em ...

The last image in the Bootstrap 4 carousel maintains a wider width than the slider itself, not resizing to fit within the slider during animations when the screen is adjusted

Check out my Bootstrap 4 carousel slider on this link. The slider consists of 3 slides, but for optimal page speed performance, I've included 5 images of different sizes that load at specific screen widths. This was achieved using the <picture> ...

The bootstrap table did not meet my expectations as I had hoped

I am currently using Bootstrap to create a basic two-column table similar to the one on the Bootstrap website here: http://getbootstrap.com/css/#tables. To achieve this, I have implemented a javascript function to display the table as shown below: $(&bso ...

"Enhancing Your Website with Dynamic CSS3 Div Animations

Seeking assistance in displaying an empty div after a CSS3 Animations loading circle has completed. Any guidance is welcome! jsFiddle ...

Unexpected behavior with Bootstrap classes

I have come across the following HTML code which appears to be using bootstrap, however, it is not responsive. It is supposed to be responsive and adapt to mobile and tablet devices, but it still displays the desktop view on mobile and tablet screens. < ...

Combining various array values into a single key in JSON格式

Issue: I am working on a sign-up form for new users using HTML. The goal is to store multiple arrays (each containing "username: username, password: password, topScore: 0) within one JSON key ("user" key). However, the current functionality only allows f ...

Issue with ngx-bootstrap custom typeahead feature malfunctioning

I'm facing an issue while trying to develop a customized typeahead feature that is supposed to search my API every time the user inputs something, but it's not functioning as expected. The autocomplete() function isn't even getting accessed. ...

The website is unable to detect the newly created .scss file

I am new to scss and currently using a Portfolio Template for my website. Recently, I added an extra section to the website. I noticed that when I define a style in an existing _file.scss file, it works perfectly fine. However, when I create a new _filenam ...

What is the reason behind the different vertical behavior of float and inline-block elements?

Check out my code example here. I've created a div with an i element inside. I set the i element's width and height attributes in a unique way, which has led to a line-height issue. While I have some knowledge about the difference between using ...

Tips for choosing text within an HTML <label> tag

Here is the HTML code provided: <label for="xxxx" id="Password_label"> <div class="xxxx">Password 555</div> 555 <div class="xxx"></div> </label> I am attempting to replace the text "555" that appears inside th ...

What is the best way to create a triangle-shaped div using CSS styling?

Seeking assistance in creating a triangular shape using CSS within a rectangular division. Grateful for any guidance provided. ...

The margin-left property is not functioning properly on the second <td> element

Displaying a table with two cells positioned next to each other: <table> <td disabled>Content for the first cell</td> <td style="margin-left:100px;" disabled>Content for the second cell</td> </table> Despite us ...

Issue with Bootstrap hamburger menu failing to collapse

Currently, I am constructing a website using Bootstrap, and unfortunately, my hamburger menu is not collapsing as intended. Below is the HTML code snippet: <section> <div class="container-fluid main-banner"> <div class=" ...

Attach image to the edge with a responsive layout using Bootstrap

I'm currently working on a project that involves the following elements: https://i.sstatic.net/rAZ0x.png I have images for the left and right sections, as well as an image of a monkey. My goal is to create a layout where the left image occupies col- ...

I'm looking to create a Slider component using @material-ui/core that has a track divided into two sections with distinct colors for the left and right sides. How

When the slider ranges from -10 to 0, I want it to display in red. For values between 0 and +10, I would like it to be shown in green. Is there a way to implement this color change using the <Slider /> component from @material-ui/core? ...

scrolling disabled in mobile browser

After testing my new site on my Android phone, I noticed that I am unable to scroll anywhere on the page. However, when viewed on Firefox, Opera, IE, and Chrome desktop browsers, I am able to scroll vertically when needed. In the CSS rule for my HTML body, ...

A panel inspired by Google Form, utilizing Material design elements and enhanced with Bootstrap features

Currently, I am in the process of developing an application using Django along with Bootstrap and incorporating Material Design CSS and JS elements from Material Design Buttons. I am seeking a simple way to replicate the functionality seen on Google Forms ...

Can I transfer JavaScript value to HTML and retrieve it using JSP?

<html> <body> <form action="javascript.jsp"> <script type="text/javascript"> var x = 15; var y = 16; <input type="hidden" name="test[0]" value=x /> <input type="hidden" name="test[1]" value=y /> </s ...

Arrange two divisions vertically in the footer

Despite being a common question, I am facing difficulties with Bootstrap 4, which may be due to some conflicts. I am trying to create two divs placed one above the other, fixed to the bottom, with text centered in both. Here is the code snippet: ...