What is the best way to create a scroll effect for a single div?

Trying to figure out how to create a scrollable div block that moves independently while the other content remains static. Currently, when scrolling down, the div moves up and creates an unwanted space between the other elements. Any suggestions on how to fix this?

HTML

<div class="wrapper">
    <div class="side-navbar">
        <h1>horse</h1>
        <h1>horse</h1>
        <h1>horse</h1>
        <h1>horse</h1>
        <h1>horse</h1>
    </div>
    <div class="content-right">
        <div class="news">
            <p>header next to side navbar</p>
        </div>
        <div class="user-content">
            <p>
                (Content Here)
            </p>
        </div>
    </div>
</div> 

CSS

.wrapper {
  display: flex;
  position: relative;
}

.wrapper .side-navbar {
  position: fixed;
  width: 200px;
  height: 97%;
  background: antiquewhite;
  padding: 30px 0;
  top: 5;
}

.wrapper .content-right {
  width: 100%;
  margin-left: 210px;
}

.wrapper .content-right .news {
  padding: 130px;
  background: red;
  position: fixed;
  width: 80%;
}

.wrapper .content-right .user-content {
  padding: 20px;
  background: red;
  margin-top: 300px;
  width: 100%;
  position: absolute;
  overflow: auto;
}

https://i.sstatic.net/WQ0ju.png Looking to isolate the scrolling functionality to just one particular div within the layout.

Answer №1

Utilizing grid display simplifies layouts like this significantly. Alternatively, flexbox could also be effective (with better support), but the underlying concept remains the same - opt for a more straightforward layout approach.

In this instance, I streamlined the layout by eliminating div.content-right, implementing grid, and incorporating a basic reset (eliminating margin, padding, and adjusting the box model to utilize the more intuitive border-box). I opted for comprehensive grid rules to facilitate easier adherence (even though shorter versions exist). As I wasn't entirely clear on the specific styles intended within each region, I made minimal adjustments apart from removing unnecessary styles due to using a grid layout.

Interestingly, Stack Overflow doesn't seem to favor the border-box solution. Codepen visually demonstrates it accurately.

// Simple reset
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

.wrapper {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr;
  grid-gap: 5px;
  height: 100vh;
  padding: 5px;
}

.wrapper .side-navbar {
  grid-row-start: 1;
  grid-row-end: 3;
  grid-column-start: 1;
  grid-column-end: 2;
  background: antiquewhite;
  padding: 30px 0;
}

.wrapper .news {
  grid-row-start: 1;
  grid-row-end: 2;
  grid-column-start: 2;
  grid-column-end: 3;
  padding: 130px;
  background: red;
}

.wrapper .user-content {
  grid-row-start: 2;
  grid-row-end: 3;
  grid-column-start: 2;
  grid-column-end: 3;
  padding: 20px;
  background: red;
  width: 100%;
  overflow: auto;
}
<div class="wrapper">
  <div class="side-navbar">
    <h1>horse</h1>
    <h1>horse</h1>
    <h1>horse</h1>
    <h1>horse</h1>
    <h1>horse</h1>
  </div>
  <div class="news">
    <p>header next to side navbar</p>
  </div>
  <div class="user-content">
    <p>
      Lorem ipsum...
    </p>
  </div>
</div>

Crafting layouts in CSS can be both fundamental and exasperating. Mastering ways to simplify them can yield significant benefits in design endeavors.

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

Calculate the total number of true values in every row

I have successfully set up a MySQL database using PHPMyAdmin to store user-selected data from a webpage. Essentially, when a user clicks buttons on the page, the corresponding column in the database changes from 0 to 1. However, I want to limit each user t ...

Troubleshooting a background image problem in a TailwindCSS configuration file

I'm having trouble getting the background image to show up correctly using tailwind.Config.Js. Even though Tailwind.Config.Js is generating the background image perfectly, when I view it in the browser, it's displaying a 404 error for the image. ...

Issue with Laravel: CKEditor text not loading HTML tags

Could you assist me with this? click here for the image description image description available here ...

Dropdown Selection Causes List Item Misalignment

Trying to figure out this issue is really testing my patience...I set up a dropdown menu that remains hidden until the user hovers over a list item. However, when hovered over, the list item shifts downward instead of staying in place. This causes the menu ...

Overflow-x in HTML5 Websites

I recently launched my website in the "alpha" stage - I have implemented full mobile support using media queries. It displays perfectly when the browser window is resized, but I noticed a strange space to the right of everything on my Samsung Galaxy Note ...

Mozilla Firefox experiencing crashes while rendering an extensive HTML table with over 20,000 rows

While attempting to render a table with over 20,000 rows, I noticed that Firefox crashes while other browsers handle it without issue. The table is generated using ASP.NET and written directly to the buffer with Response.Write. Swapping out the direct HTML ...

How can I use TailwindCSS in NextJS to remove the "gap" className from a component?

Currently, I am working on button components in NextJS with Tailwindcss and I am encountering a problem with some variants. Everything works fine, except when I remove the children (button text), I notice something strange that I can't quite figure ou ...

transforming the use of getelementbyid into a plural form

Can a getElementById function work in plural context? Or would I need a different function or maybe jQuery? <script type="text/javascript> window.onload = function() { var test = document.getElementById('test'); if ...

Unable to locate any division element by using document.getElementById

I'm encountering an unusual issue in my code. I am attempting to change the background color of certain divs using Javascript, but for some reason, when I use "document.getElementById", it is unable to find the div and returns NULL. Here is the probl ...

Automatically switch/animate CSS classes at defined intervals using jQuery

In the PHP code snippet below, three list items are generated and a CSS class "active" is added/removed on mouseover/mouseout. The JavaScript provided toggles the "active" class for all list items at once when moused over, but the requirement is to anima ...

Failed to update the innerHTML attribute for the anchor tag

I'm attempting to apply styles through DOM manipulation using Angular's renderer2. I have successfully updated styles for all HTML elements except for anchors. In the example below, I am trying to replace the text www.url.com with World within ...

Finding an element based on its styling attribute, such as its position on the left or right side

One particular block that caught my eye is the slider element: <div id="sliderDispo" class="slider slider-dispo" data-slider-init="" data-slider-color="#0077b5 #EC6E31 #E40B0B" data-slider-step="33" > <div class="slider__interval" ...

The div-tag is not displaying the background image as expected

I'm completely stumped. My goal was to create my own gallery, but right from the start, I hit a roadblock: Trying to display an image as a background in a <div>. I've tried everything, searched high and low on the internet, combed through s ...

What is the best way to make a child div's height match its parent's height?

I am facing an issue with aligning 2 divs on the same line, separated by a vertical line while ensuring that the line always has the height of the parent div. Despite trying various solutions suggested online, none seem to work for me. I cannot use positi ...

Dynamic binding issue causing audio event to not fire

Currently, I have an <audio> element that is playing audio. When I manually add the event listener onplay in the HTML code, it functions as expected. <audio onplay="alert('t')" .... It's working... However, when I attempt ...

class=btn btn-danger align-horizontal

I need help aligning my 3 buttons horizontally side by side. The current setup doesn't look good to me. class= btn btn-danger Can someone guide me on how to write the CSS for this? This is my code: <div class="row"> <div class="co ...

Steps for incorporating code to calculate the total price and append it to the orderMessage

I am seeking help with this program that my professor assigned to me. The instructions marked by "//" are the ones I need to implement in the code, but I'm struggling to understand how to proceed. Any assistance would be greatly appreciated, even just ...

A div element set to a width of 100% that holds a lengthy text will expand to the entire width of the window,

Check out this simple webpage: https://jsfiddle.net/enxgz2Lm/1/ The webpage is structured with a side menu and a tasks container. Within the tasks container, there is a row container that includes a checkbox, title on the left, and details on the right. ...

choose a div with additional padding at the top

I am looking to create a dropdown menu with a custom arrow design. I have attempted to achieve this by wrapping my select element in a div with an arrow image background. The issue I am facing is that the select element seems to have 3-4 pixels of top p ...

Shifting the menu to the left side of the header for mobile devices: A step-by

I am looking to switch the location of the "hamburger" menu from the right side of the page to the left side. The website in question can be viewed here. This webpage is using the "Bulk" theme. The CSS styling for the icon is as follows: .open-panel { ...