Ways to stop the side navbar from scrolling

Instead of having a separate scroll for the side navbar, I would prefer to have it scroll along with the main body. This way, when you scroll through the main content, the additional side navbar information will also be displayed.

For an example, you can check out this link: https://jsfiddle.net/wzcya8b9/3/

HTML

<div class="sidenav">
  <a href="#about">About</a>
  <a href="#services">Services</a>
  <a href="#clients">Clients</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="#services">Services</a>
  <a href="#clients">Clients</a>
</div>

<div class="main">
  <h2>Sidebar</h2>
  <p>This sidebar is of full height (100%) and always shown.</p>
  <p>Scroll down the page to see the result.</p>

</div>

</body>

CSS

.sidenav {
  height: 100%;
  width: 160px;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  padding-top: 20px;
}

.sidenav a {
  padding: 6px 8px 6px 16px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
}


.main {
  margin-left: 160px; /* Same as the width of the sidenav */
  font-size: 28px; /* Increased text to enable scrolling */
  padding: 0px 10px;
}

Answer №1

Are you interested in a design similar to this one?

body {
  font-family: "Lato", sans-serif;
  margin:0
}

.container{
  display:flex
}

.sidenav {
  height: 100%;
  width: 160px;
  position: sticky;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  /*overflow-x: hidden;*/
  padding-top: 20px;
}

.sidenav a {
  padding: 6px 8px 6px 16px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.main {
  margin-left: 160px; /* Same as the width of the sidenav */
  font-size: 28px; /* Increased text to enable scrolling */
  padding: 0px 10px;
}
<div class="container">
<div class="sidenav">
  <a href="#about">About</a>
  <a href="#services">Services</a> 
    (Repeated content for brevity)
</div>

<div class="main">
  <h2>Sidebar</h2>
  <p>This sidebar is of full height (100%) and always shown.</p> 
    (Lorem ipsum content repeated for demonstration purposes)
</div>
</div>

Answer №2

One option to consider is utilizing either css grid or Flexbox for your layout.

To gain a deeper understanding and see practical examples, check out this informative article.

https://alligator.io/css/css-grid-holy-grail-layout/

SIMPLE DEMO

An example implementation could involve creating a 200px column alongside a dynamic column that fills the remaining width of the page. The navigation bar could be positioned on the left side with a shared scroll bar.

HTML

<div class=“wrapper”>
    <div class=“sidebar”></div>
    <div class=“main”></div>
</div>

CSS

.wrapper {
    display: grid;
    grid-template-columns: 200px auto;
}

If you prefer having the navbar remain at the top of the window as users scroll, consider replacing position:fixed with position:sticky.

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

The responsive navigation bar yielded an unforeseen outcome

Looking to create a responsive navigation bar similar to the one shown here My code successfully shows and hides the navigation items, but it's not updating the burger icon No console errors or warnings are present This is my HTML: <nav> ...

Displaying a message text upon successful AJAX request

I have a web page with an AJAX request that updates data in the database. After the update, I want to display a message to the user on the webpage confirming that the data has been successfully updated. However, currently, no message is being displayed and ...

Divide the screen evenly with a split, where one side is filled with an image

As a newcomer, I'm curious about how to split the screen in half with a form on the left and an image taking up the full height on the right. Is there a simple way to achieve this? For reference, here is a link showing exactly how I envision it: Exam ...

How can you horizontally align a collection of elements using CSS?

I'm struggling to align a paragraph element with a group of button elements using jQuery and CSS. This is my issue: My goal is to have all these elements on the same horizontal line at the top of the screen to make the most of the pixel real-estate. ...

Maintain the grouping of elements within a div when printing in Internet Explorer 8

When printing in IE8, I'm facing an issue with keeping the "Table title" line on the same page as the <table>. Despite using page-break-inside:avoid;, there is still a page break between the table title and the actual table. My expectation is f ...

In JavaScript, I'm facing an issue where I can't seem to use ' ' for line breaks for some reason. It's not functioning as expected, and I'm

It seems that I have been struggling with writing a new line in my code using "\n". Despite multiple attempts, the desired outcome has not been achieved. Below is my source code along with a screenshot of the result: var ary3 = new Array('seve ...

Enhancing Vue with stylish components

My Vue component is quite basic, with the following structure: <script> export default { props: { source: { type: String, required: true, } }, } </script> <template> <div class="imageItem"> <img class="image" :data-srcse ...

Traversing hrefs inside list elements with Python Selenium

When parsing a webpage with an organization structure like the one below: <nav class="sidebar-main"> <div class="sidebar">Found 3 targets</div> <ul><li><a href="#target1" class="current"><span>target1& ...

Asynchronous JavaScript function within a loop fails to refresh the document object model (DOM) despite

I have been working on a function that utilizes ajax to retrieve instructions from a backend server while the page is loading. The ajax code I've written retrieves the instructions based on the number provided and displays them using the response.setT ...

Unusual patterns appearing in HTML image files

As a beginner in html, please pardon my silly questions and mistakes. I have designed this webpage: <!DOCTYPE html> <html> <head> <meta charset = "UTF-8"> <style type = text/css> #topbar { ...

Shoot back two victories in ajax

I am looking to split the output obtained after making an ajax request <input type="text" class="form-control" name="userno" id='stud_id' readonly > <input type="text"name="studentname" id='studentname' readonly > <inpu ...

Having trouble positioning grouped buttons in jQuery Mobile?

In the controlgroup container, I have included the data-type="horizontal" attribute. This container contains a select menu and a plus icon, giving the user the option to select an existing category or add a new one. I am looking to position the "+" icon in ...

Is it possible to capture the child element's id?

After loading several images onto the page, I hide them using 'display:none': <div id="cont-img"> <img class="lista-img" src="list/1.png" id="v1" /> <img class="lista-img" src="list/2.png" id="v2" /> <img class=" ...

Tips for invoking a controller method in HTML code

Hello, I am completely new to AngularJS, HTML, JavaScript, and CSS. Please keep your explanations simple for beginners like me. I'm facing an issue where the function "updateFilterTerm" is not being called, causing the variable "filterTerm" to remain ...

Template builder for editing forms

I have successfully created a form creation page, but now I need to create a form edit page. However, I can't seem to find the necessary attribute for it. How should I proceed? Rendering a form for the admin panel image description goes here var op ...

HTML declaration vandalized site

There's a strange bug I've encountered with the use of !DOCTYPE html. Whenever I attempt to implement the HTML5 Doctype, my page ends up looking distorted. It seems like all scripts are not properly closed. However, when I switch to !DOCTYPE ht ...

Unable to modify jwplayer css styles to customize the chromecast icon

Is there a way to customize the chromecast icon within the JWPlayer skin to have a specific color? I've identified that the styles I need to change are --connected-color and disconnected-color when inspecting the element. Despite my attempts through ...

How to send parameters to Bootstrap modal using a hyperlink

I need help confirming the deletion of a datatable row using a Bootstrap modal dialog. When a user clicks on the delete link, I want a modal to appear asking for confirmation. Here is my code: <c:forEach items="${equipes}" var="equ"> ...

The top border of the body element is contained within the viewport and does not

Looking to add a purple line across the top of my website without using an image. I've opted for the border-top property on the body element. However, when resizing the browser window, the purple line only remains in the viewport area, resulting in a ...

Creating interactive maps with dynamically added area tags using JavaScript

I am looking to dynamically load an image and add a map tag to it. The coordinates of different areas (such as circle coordinates) are coming from a database and I need to add those areas to the map tag dynamically using JavaScript or jQuery in a loop. I ...