Scroll on sidebar that overflows, rather than the page behind

My goal is to have a fixed background page while allowing the sidebar to scroll independently. Both elements are too large for the screen, so I want the page to scroll normally until the navbar overflows in height and becomes scrollable. Check out the example here:

https://jsfiddle.net/tpL92b01/2/

The grey div should be able to scroll.

This demo does not involve the typical show/hide functionality of the sidebar.

body, p, ul {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

p{
  padding: 30px;
  text-align: center;
  border: 1px solid blue;
}


.main-content {
  background-color: lightblue;
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: 200px;
}

.sidebar ul {
  background-color: lightgrey;
}

.sidebar li {
  padding: 15px;
}
<html>
  <body>
    <div class="main-content">
      <p>Some content</p>
      <p>Some content</p>
      <p>Some content</p>
      <p>Some content</p>
      <p>Some content</p>
    </div>
    <div class="sidebar">
      <ul>
        <li>Menu item1</li>
        <li>Menu item2</li>
        <li>Menu item3</li>
        <li>Menu item4</li>
        <li>Menu item5</li>
        <li>Menu item6</li>
        <li>Menu item7</li>
        <li>Menu item8</li>
        <li>Menu item9</li>
      </ul>
    </div>
  </body>
</html>

Answer №1

Consider implementing a max-height and overflow:auto for the sidebar to enable scrolling only when necessary.

body, p, ul {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

p{
  padding: 30px;
  text-align: center;
  border: 1px solid blue;
}


.main-content {
  background-color: lightblue;
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: 200px;
  max-height: 100%;
  overflow: auto;
}

.sidebar ul {
  background-color: lightgrey;
}

.sidebar li {
  padding: 15px;
}
<html>
  <body>
    <div class="main-content">
      <p>Some content</p>
      <p>Some content</p>
      <p>Some content</p>
      <p>Some content</p>
      <p>Some content</p>
    </div>
    <div class="sidebar">
      <ul>
        <li>Menu item1</li>
        <li>Menu item2</li>
        <li>Menu item3</li>
        <li>Menu item4</li>
        <li>Menu item5</li>
        <li>Menu item6</li>
        <li>Menu item7</li>
        <li>Menu item8</li>
        <li>Menu item9</li>
      </ul>
    </div>
  </body>
</html>

Answer №2

modify this CSS snippet

.navigation {
  position: absolute;
  overflow: hidden;
  max-height: 90%;
  top: 10px;
  left: 20px;
  width: 250px;
}

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

Site Having Trouble Displaying Image

I'm currently working on a Bridgetown website using Bootstrap 5, and I've encountered an issue with adding an image that won't show up on the site. This is what it currently looks like: https://i.sstatic.net/IUR7x.jpg Code <nav class=&quo ...

PHP - session expires upon page refresh

I'm in the process of creating a login system for my website and I've run into an issue with updating the navigation bar once a user has logged in. Every time I refresh the page, it seems like the session gets lost and the navigation bar doesn&ap ...

React modal image showing a misaligned image upon clicking

I recently integrated the react-modal-image library into my project to display images in a modal when clicked. However, I encountered an issue where the displayed image is off center with most of it appearing offscreen. I'm unsure what is causing this ...

Setting header details for optimal data transfer

Currently, there is a Java code snippet that I am working on which attempts to access the header information sent by an HTML page. Unfortunately, I do not currently have the specific HTML page in question. However, I still need to be able to access and m ...

Sending a value to a text box using json data transmission

I am having trouble accessing data from a js file and fetching the value to an html text box. Despite trying, I'm unable to get the desired result. Below are the contents of samle.js file and jsonhtml.html file: { "var1": "1", "var2": "2" } <scri ...

Grid of domes, fluid contained within fixed structures and beyond

I've been grappling with this issue for a while now, and have had to rely on jQuery workarounds. I'm curious if there is a way to achieve this using CSS or LESS (possibly with javascript mixins). The page in question consists of both fixed and f ...

What is the best way to utilize CSS to center an element in relation to the scroll bar?

For a group project, I successfully centered a div on a webpage. However, I ran into an issue where the website's contents are centered taking the scroll bar width into consideration. This means that the web page contents adjust to be centered without ...

Tips on personalizing the DateTimePicker component from Material-UI

I am currently working on customizing the DateTimePicker component provided by Material-UI. To access its documentation, please visit: One challenge I have encountered is the lack of styling information in the documentation. My goal is to change the main ...

Guidelines on creating actionable functions using JavaScript

Having trouble connecting the form to the database and looking for a solution. This is the HTML form that I've been working on, attempting both POST and GET methods. <form id="register" action="script/register.js" method="post"> <label for= ...

Exploring Alternative Methods to Navigate Through Elements Using JQuery UI Autocomplete

After testing two different approaches: <div id = "team-search-container"> <label for="team-search" class = "text-center"> <input type = "text" id = "team-search"> </label> </div> With the following code sni ...

Looking for an API to retrieve random quotes and images from a website?

Greetings, my name is Antika. I recently embarked on a coding journey and have been focusing on learning HTML/CSS along with the basics of JS. Level of Expertise: Beginner During my exploration, I stumbled upon this intriguing website - . It stands out a ...

Receiving JSON information from a web address using Javascript

I'm currently faced with a challenge in extracting JSON data from a web server. Despite the absence of errors in my code, I encounter difficulties displaying any output. Below is a snippet of the issue: <!DOCTYPE HTML> <html> <head ...

Map region indicator tooltip

I possess a map that contains a specific area with a title. Here is the accompanying code: <map name="Map" id="Map"> <area alt="" shape="rect" coords="127,52,186,71" href="#" title="Extending telephone lines to other Egyptian governorates ...

What is the best method for converting Unix time to a readable date

<el-table-column label="Time Created" prop="create_time"></el-table-column> https://i.stack.imgur.com/aoLjf.png The timestamp data from the backend is in milliseconds format (e.g. 1527150668419) and is stored as create_time property in this.i ...

Hover over the drop-down menu to enable tab on hover mode

Is it possible to make the tab switch to hover mode when I rollover the drop down sub-menu without using JavaScript, and only using CSS? <li id="anchor" class="title dropdown"><a href="#">Main Tab</a> <div class="co ...

Angular 1.5 component using HTTP GET

Trying to utilize a 1.5 component with AngularJS has presented some challenges for me. I have a service that fetches my JSON file using $HTTP and returns a promise. In the controller of my component, I resolve the promise and assign it to a value using thi ...

Tips for identifying a webpage and making class modifications based on it

I am working on a customized bar with 4 distinct parts. Each part corresponds to a different page on the website. I want each part of the bar to change color based on which page the user is currently browsing. For example, if the user is on page A, I want ...

Identifying the HTML Hidden Attribute Using JavaScript Without Dependencies

As someone working in the analytics field, I often rely on CSS selectors to address various issues. Currently, I am faced with a task on a website where I need to determine whether a <p> element is hidden or visible. There are two possible scenarios: ...

Inspecting JavaScript for any attachments

Before hitting the submit button, I need to verify if a file has been uploaded and display a warning message prompting the user to attach a file if it hasn't been done yet. I am looking for guidance on how to achieve this using JavaScript, Prototype, ...

Launching a modal in a new browser window with the help of JavaScript or PHP

I need to implement a feature where clicking a link opens a modal in a new tab and redirects the current page to a different link, similar to how retailmenot handles coupons. Here is the code snippet I am currently working with: <div onClick="myFunctio ...