Display the scrollbar on a flexbox container instead of the entire browser window

I'm curious about how to implement a scroll-bar on a div using flexbox CSS.

To better illustrate the issue, I've provided an example below:

* {
  box-sizing: border-box;
}
.flex-display {
  display: flex;
  align-items: stretch;
  height: 100%;
}
.tree-container {
  border: 1px groove gray;
  padding: 10px;
  width: 25%;
  height: 100%;
}
.detail-container {
  border: 1px groove black;
  padding: 10px;
  width: 75%;
  height: 100%;
  flex: auto;
}
#ViewContainer {
  display: flex;
  flex-direction: column;
  align-items: stretch;
}
#Header {
  border: 1px ridge;
  display: block;
}
#Detail {
  border: 1px ridge;
  overflow-y: auto;
}
<div class="flex-display">
  <div class="tree-container">
    HERE COMES A TREE
  </div>
  <div class="detail-container">
    <div id="MainContainer">
      <div id="ViewContainer">
        <div id="Header">
          This is going to be my Header View with Fixed Height of Content.
        </div>
        <div id="Detail">
          <h2> Set scrollbar on this div to utilized remaining height of browser window without showing scrollbar on Browser.</h2>

          <div>
            <p>
              HEre we are going to have some divs or some form and some other things but this part has to be scrollable.
            </p>
          </div>

          <div>
            <p>
              HEre we are going to have some divs or some form and some other things but this part has to be scrollable.
            </p>
          </div>

          /* Remaining code here */

        </div>
      </div>
    </div>
  </div>
</div>

Click the link to view it in the pen. CodePen

If you encounter a similar situation, check out this resource: Scrolling a flexbox with overflowing content

I would greatly appreciate any input or suggestions regarding this matter.

Answer №1

Here are some tips to improve your code:

  • Make sure to eliminate the default margin on the body element

    body { margin: 0; }
    
  • Adjust the scrollbar div to be 100% of the viewport height, minus borders and padding

    #ViewContainer {  height: calc(100vh - 26px); }
    

    The calculation for height is: (viewport height - 20px padding - 6px borders)

Check out the revised Codepen here

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

If the overflow-x property is set to hidden for the html and body elements, the links in the footer will become unclickable as they will

Situation: Consider the following scenario with a simplified HTML example: position the footer behind the content and make it stick to the bottom when reaching the end of the page, the footer should slide up from behind the content I achieved this layo ...

Preventing repetition of data and storing submitted form inputs in a cache

I am facing an issue with my code that checks for duplicate entries based on the USER ID in the database. When I submit the form and the USER ID already exists, it clears all other fields on the form, requiring me to re-enter all details again. This is fru ...

What is the best way to insert a new row into a table upon clicking a button with Javascript?

Hi everyone, I'm facing an issue with my code. Whenever I click on "Add Product", I want a new row with the same fields to be added. However, it's not working as expected when I run the code. Below is the HTML: <table class="table" id="conci ...

When clicked, the menu disappears successfully with the "show" functionality, but unfortunately, the "hide" feature is not working as intended

Within my menu, I have a section for portfolios that transforms into a dropdown on desktop hover and mobile click. The issue arises on mobile devices where the first click displays the menu correctly, but subsequent clicks do not trigger any change. I att ...

The area surrounding inline content, both above and below

Can you explain the space above and below inline content? This phenomenon can be seen here: http://jsbin.com/vertical-spacing-weirdness/ If you look closely, there is a white area between the div/table and the span even though the span does not have ...

Learn how to create a responsive flickr embedded video with Bootstrap!

I'm struggling to make the embedded video responsive. I attempted the solution mentioned in a Stack Overflow post but it didn't work for me. The video's width remains the same and doesn't scale down properly, appearing out of bounds on ...

Challenges with looping in Jquery animations

I've been working on an animation function that I want to run in a loop. So far, I've managed to get it to work for one iteration, but I'm struggling to figure out how to repeat the loop a specific number of times. Below is the code for my a ...

CSS3 rotation animation - begins to rotate and then halts at a specific angle

I attempted to develop a function that initiates a spin, replaces an image, and then stops the spin. However, when I remove the spin class, it jerks abruptly. How can I halt the spinning smoothly at a specific frame? setTimeout(function() { $('. ...

HTML - one div's child element takes precedence over another div

My HTML page features two consecutive divs. The first div contains a child span that has a relative position, causing it to overlap the second div. I have implemented click events for both divs. However, when I click on the span that overlaps the second d ...

Ensuring a full height div using CSS

I have encountered an issue and created a fiddle to help illustrate it: http://jsfiddle.net/XJpGT/ The challenge I am facing is ensuring that the height of the green box always remains at 100%, while also making sure that the green and orange boxes do not ...

I'm experiencing some unusual behavior with the Windows media app when using HTML and CSS

I have a Windows Media Player box on my page, but it overlaps every piece of HTML code. How can I get it to move to the back so that I can still use it? Another problem is that everyone who visits my page needs a plugin to load it, even though most people ...

Interactive Video Effect

Seeking to create a unique video hover effect on an image where, when the mouse hovers over it, a video pops up similar to examples found on this website. I have been grappling with finding a solution for this issue over the past few months. Does anyone ...

What is the best way to center align the div container horizontally?

Trying to center the #container but all methods have failed. How can I achieve centering using only CSS? body { text-align:center; } <div id="app"> <div id="container"><div id="container_red" style="position:absolute;left:0"> ...

Error: The function m.easing[this.easing] is not defined

I have been working on creating anchor link scrolling and tooltip display using bootstrap, but I am encountering an issue. $(window).scroll(function(){ if ($(window).scrollTop() >= 100) { $('#header').addClass('fixed'); ...

The width of table columns is altered upon the second rendering in JQuery DataTables

Is there a way to prevent JQuery DataTables cells from extending width after adding hyperlink to cell? I am facing an issue where I have two tables on the same page. The first table column has a link which populates the second table. It works fine when th ...

To customize or not to customize?

Lately, there has been a growing trend of people incorporating custom attributes into their HTML tags to add extra data for use in JavaScript code. I'm curious to hear thoughts on the practice of using custom attributes and what alternatives are avai ...

HTML/CSS code for a header with elements centered across the full width of the screen

Having an outdated WordPress theme from 2010, I wanted to give it a modern touch by adding a sticky header. I found a plugin called myStickymenu that seemed to work well. However, my current theme's header has a fixed width of 960px and I desired to m ...

Issues with padding and margin not displaying correctly at different screen sizes

I'm currently utilizing tailwindCSS and am facing an issue with adjusting the size of buttons based on screen sizes. I want the buttons to appear small with minimal vertical padding on desktop screens, and bigger with increased vertical padding on mob ...

Unusual Glitch in Bootstrap 3 Dropdown Menu

I am currently developing a website using Bootstrap 3. I am encountering an issue with the navbar dropdown. When I click on the link, it changes to show "expand child menu" and "collapse child menu". To clarify, here is the image I am referring to: Initi ...

How can I replace the unsightly "Web page not available" error in WebView on Android with a more visually pleasing alternative?

In my WebView Activity, I sometimes encounter issues with loading properly when the WIFI/DATA connection is poor or disconnected. This could potentially be a common occurrence once my app is in use. I'm curious to know how I can replace the unattracti ...