Prevent children from extending outside the boundaries of their parent

I am facing an issue with my page layout that includes a sidebar. The content in the sidebar is overflowing the page, so I attempted to use CSS properties like overflow-y: hidden and max-height: 100vh on the main element, as well as overflow-y: auto on the list group to enable scrolling. However, this solution did not work as intended. Despite cutting off the overflowing content, the scroll bar was not visible.

main {
  height: 100vh;
  max-height: 100vh;
  overflow-y: hidden;
}

.scrollarea {
  overflow-y: auto;
}
<html>

<head>
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedcd1d1cacdcaccdfcefe8b908d908e93dfd2ced6df8f">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e7eaeaf1f6f1f7e4f5c5b0abb6abb5a8e4e9f5ede4b4">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
</head>

<body>
  <main>
    <header class="col-12 navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
      <div class="navbar-nav ms-auto">
        <div class="nav-item text-nowrap justify-self-end">
          <a class="nav-link px-3" href="#">Sign out</a>
        </div>
      </div>
    </header>

    <div class="container-fluid d-flex flex-column">
      <div class="row flex-grow-1">
        <div class="col-md-4 border-end gx-0 d-flex flex-column flex-shrink-0">
          <div class="d-flex justify-content-center mt-2 border-bottom">
            <h2>Placeholder</h2>
          </div>
          <div class="list-group list-group-flush scrollarea">
            (Content of the list group here)
          </div>
        </div>
        <div class="col-md-8">
          main content
        </div>
      </div>
    </div>
  </main>
</body>

</html>

I'm seeking guidance on how to create a scrollable sidebar using Bootstrap classes efficiently. Any suggestions would be highly appreciated.

Answer №1

Discover a handy solution utilizing the flexbox technique along with strategic use of the position property to achieve your desired layout. It’s worth noting that while this sample provides helpful insights, it does not cover responsive design concerns for smaller screens.

  1. Begin by employing flex CSS to establish the structure of your layout (header, sidebar, main content).
  2. Utilize the position property on the scrollable area: an absolute box inside a relative container.

Give this approach a try and enjoy your coding journey!

main {
  height: 100vh;
}
<html>

<head>
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2808d8d969196908392a2d7ccd1ccd2cf838e928a83d3">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0c2cfcfd4d3d4d2c1d0e0958e938e908dc1ccd0c8c191">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
</head>

<body>
  <main class="d-flex flex-column">
    <header class="col-12 navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
      <div class="navbar-nav ms-auto">
        <div class="nav-item text-nowrap justify-self-end">
          <a class="nav-link px-3" href="#">Sign out</a>
        </div>
      </div>
    </header>

    <div class="flex-grow-1 container-fluid d-flex flex-column">
      <div class="row flex-grow-1">
        <div class="col-md-4 border-end gx-0 d-flex flex-column">
        
          <div class="d-flex justify-content-center mt-2 border-bottom">
            <h2>Placeholder</h2>
          </div>
          
          <div class="flex-grow-1 position-relative">
            <div class="position-absolute w-100 h-100 overflow-y-auto">           
              <div class="list-group list-group-flush scrollarea">
                <a href="#" class="list-group-item list-group-item-action active">
                  <div class="">
                    <div class="d-flex align-items-center justify-content-between">
                      <strong>Title</strong>
                      <span class="badge rounded-pill bg-danger">10</span>
                    </div>
                    <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
                  </div>
                </a>
                
                <!-- More similar list items here -->
              
              </div>
            </div>
          </div>
          
        </div>
        <div class="col-md-8">
          main content
        </div>
      </div>
    </div>
  </main>
</body>

</html>

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

Tips for using jQuery to add or append a class to the final li element

I am currently struggling to understand why the last child of the li element is not being inserted into the last li. This module functions as a treeview, and my main objective is to add a class to the last li. Let me demonstrate with some sample code and ...

What is the best way to delete spaces between span elements while preserving the spaces within each individual span tag?

Is there a way to eliminate unexpected spaces that appear between spans? One attempt was made by setting font-size: 0 within the wrapper <div>, which successfully removed spaces not only between <span> tags but also within each <span> ta ...

Eliminate any unnecessary or hidden spacing on the left side of the options within an HTML select

Currently, I am in the process of creating a Registration Form and encountering a challenge with aligning the input[type="text"] placeholder and the "pseudo" placeholder for a <select> element. My goal is to have both placeholders left-aligned flush ...

What is the best way to enable CSS IntelliSense for a Nuxt project?

Currently, I am undertaking a project using Nuxt.js and have set up a Docker image to manage all aspects of it. Additionally, I am looking to integrate MDBVue as a CSS library into my project. After some research, I learned that in order to utilize this li ...

What are some tips for getting media queries to function properly?

I need help with my homepage banner image as I am trying to make it responsive by adding media queries. The goal is for the image to adapt to 3 different sizes based on the screen being used, but currently only the medium-sized version appears. Could someo ...

What is the best way to ensure consistent spacing between columns in a Bootstrap 3 grid layout, similar to the spacing on the left and right sides?

Is there a way to achieve equal spacing between all blocks in a row, similar to the left and right padding that Bootstrap automatically applies? Currently, Bootstrap adds padding to all columns on both sides, but does not render extra spacing on the left s ...

adjust the width of an element based on the size of characters within it

I need to ensure that the width of a div is equivalent to 10 characters If one character is equal to 2px, then I want the width to be 20px, even if the text is only 4 characters long. <div class="row"> <div class="key">City</div> ...

Material UI grid with 2 items: one aligned to the left and one aligned to the right in a single line

I'm struggling to understand how to effectively utilize Material UI's flexbox integration. I want to align items in a specific way, like this: export default function Home() { return ( <Grid container justify={"space-between&q ...

Tips on connecting a file upload button to a flip card

In my current project located in index.html, I have integrated a flip card as a profile photo feature. Additionally, I have included a file uploader button on the page. However, I am struggling to connect the functionality of the file uploader button to au ...

A step-by-step guide on using Jquery to fade out a single button

My array of options is laid out in a row of buttons: <div class="console_row1"> <button class="button">TOFU</button> <button class="button">BEANS</button> <button class="button">RIC ...

Progressing through the Material UI LinearProgress bar in steps

How can I split a progress bar into more steps to achieve a design like this? https://i.stack.imgur.com/lRMj6.png I attempted using this code but couldn't find an option for splitting: <LinearProgress variant="determinate" classes={{ ...

Highlight the active page or section dynamically using HTML and jQuery - How can it be achieved?

What am I trying to achieve? I am attempting to use jQuery to dynamically highlight the current page from the navigation bar. Am I new to jQuery/HTML? Yes, please excuse my lack of experience in this area. Have I exhausted all resources looking for a sol ...

Is it Possible to Customize the Font Color of the Bootstrap Disabled Class in Bootstrap 4?

I am currently using version 4.4.1 of the Bootstrap gem for my Rails application which includes a Bootstrap 4 navbar. Below is a snippet from my navbar code: <ul class="navbar-nav ml-auto"> <!-- Hidden li included to remove active class fro ...

ReactJS Application: Issue with Selective Mobile Scrolling

I've been working on a ReactJS web app where we mainly use styled-components for styling, but also sometimes utilize index.css for global styles (such as body and html). The application consists of an app header, a page header, and a container with a ...

PHP Bootstrap Confirmation Dialog Tutorial

Is there a way to implement a delete confirmation dialog? When 'yes' is clicked, the message should be deleted, and when 'no' is clicked, the delete operation should be canceled. At present, this is how it looks in my view: <a href ...

Tips for maintaining the particles.js interaction while ensuring it stays under other elements

I have incorporated particle.js as a background element. By adjusting the z-index, I successfully positioned it in the background. While exploring solutions on the Github issues page, I came across a suggestion involving the z-index and this code snippet: ...

Struggling to locate a straightforward sidebar solution without relying on Bootstrap. Finding an easy-to-use answer seems

I have a lightweight and easy-to-understand code for a website project with two panels. The first panel on the left is a large navigation (270px width, top to bottom, similar to a wiki) with approximately 30 unordered list items. Next to it is the second ...

The vertical spacing in Font "bottom-padding" appears to be larger than anticipated

Recently, I purchased the font Proxima Nova for my project and I am encountering issues with vertical alignment. Despite setting margins, line-heights, and padding to match those of Arial, the results are not consistent as the Proxima Nova appears misalign ...

Arranging elements on a webpage using CSS grid placements

Currently experimenting with css grid, envisioning a scenario where I have a 2x2 grid layout. When there are 4 child elements present, they would form 2 rows of 2 elements each. My challenge is when dealing with an odd number of children, I aim to avoid al ...

"Is an Ionic Top Navigation Bar the best choice for your

Creating an Ionic Top Navigation Bar Greetings! I am facing some challenges while trying to implement a Top Navigation Bar in my Ionic Project. Despite the lack of documentation on this specific topic, I am struggling to make it work properly. Your as ...