Unable to synchronize the scrolling of two divs

Visit this link to view the code snippet.

I am dealing with a parent container that contains two scrollable blocks, one of which is absolutely positioned. This layout is necessary in order to create a wrapper with border-radius properties. Both blocks are guaranteed to have the same width.

.container {
  width: 400px;
  border: 1px solid #ccc;
  height: 100px;
  overflow: auto;
  margin: 0 auto;
  position: relative;
  font-size: 18px;
  white-space: nowrap;
}

.head {
  overflow: auto;
  height: 40px;
}

.container .body {
  border: 2px solid #c4c;
  position: absolute;
  border-radius: 16px;
  right: 0;
  left: 0;
  bottom: 0;
  height: 50px;
  overflow: auto;
}
<div class="container">
  <div class="head">
    we like it, but a person who isn't familiar with his story may have some difficulty seeing how it would be so fitting, but it's hard to ignore the similarities between this dog and cat.
  </div>
  <div class="body">
    we like it, but a person who isn't familiar with his story may have some difficulty seeing how it would be so fitting, but it's hard to ignore the similarities between this dog and cat.
  </div>
</div>

My attempt to synchronize the scrolling behavior using JavaScript resulted in flickering and did not feel like an ideal solution. I am seeking a method to make both scrollable blocks move synchronously as if they were one block, without relying on JavaScript. Is there a way to achieve this?

Answer №1

One potential solution could be to:

  • implement only one overflow at the container level
  • handle the wrapper in the body section as a container::before element
  • give the wrapper a position: sticky
  • set the body and head with a position: absolute to position them vertically

.container {
  width: 400px;
  border: 1px solid #ccc;
  height: 100px;
  overflow: auto;
  margin: 0 auto;
  position: relative;
  font-size: 18px;
  white-space: nowrap;
  overflow: auto;
  position: relative;
}

.container::before {
  content: ' ';
  border: 2px solid #c4c;
  position: sticky;
  border-radius: 16px;
  display: block;
  left: 0;
  top: 50%;
  bottom: 0;
  height: 50%;
  width: 100%;
  z-index: 2;
}

.body, .head {
  position: absolute;
}

.body {
  top: 15%;
}

.head {
  top: 65%;
  z-index: 3;
}
<div class="container">
  <div class="head">
    we like it, but a person who isn't familiar with his story may have some difficulty seeing how it would be so fitting, but it's hard to ignore the similarities between this dog and cat.
  </div>
  <div class="body">
    we like it, but a person who isn't familiar with his story may have some difficulty seeing how it would be so fitting, but it's hard to ignore the similarities between this dog and cat.
  </div>
</div>

To differentiate the background of the container and wrapper, adjust the z-index to allow the body to have a greater value than the wrapper's z-index

.container {
  width: 400px;
  border: 1px solid #ccc;
  height: 100px;
  overflow: auto;
  margin: 0 auto;
  position: relative;
  font-size: 18px;
  white-space: nowrap;
  overflow: auto;
  position: relative;
  background: blue;
}

.container::before {
  content: ' ';
  border: 2px solid #c4c;
  position: sticky;
  border-radius: 16px;
  display: block;
  left: 0;
  top: 50%;
  bottom: 0;
  height: 50%;
  width: 100%;
  background: yellow;
  z-index: 2;
}

.body, .head {
  position: absolute;
}

.body {
  top: 15%;
}

.head {
  top: 65%;
  z-index: 3;
}
<div class="container">
  <div class="head">
    we like it, but a person who isn't familiar with his story may have some difficulty seeing how it would be so fitting, but it's hard to ignore the similarities between this dog and cat.
  </div>
  <div class="body">
    we like it, but a person who isn't familiar with his story may have some difficulty seeing how it would be so fitting, but it's hard to ignore the similarities between this dog and cat.
  </div>
</div>

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

VueD3tree successfully fetches data, however, it is not being displayed on the screen

Just started delving into Vue and VueD3tree. Attempting to display a tree using the vued3tree library with data pulled from an API. Strangely enough, when trying with static data, the tree displays perfectly fine. However, when fetching data dynamically, a ...

What could be causing my table to malfunction in IE8?

Is it just me, or do my CSS tables not work on IE8? <div class="main-table"> <div class="row"> <a class="secondary-table" href="#"> <div class="secondary-row"> <div class="secondary-cell"> ...

The div is not showing the image as expected

Having some trouble creating a slideshow within my angular application. Struggling to get the images to display in the html code. To tackle this, I decided to create a separate component specifically for the slideshow (carousel.component). In the main app ...

Is there a way to conceal the scrollbar while still permitting scrolling?

I'm interested in creating a custom scrollbar, but in order to do so I need to hide the default scrollbar while still allowing scrolling functionality. I've attempted: overflow-y:hidden; but this method hides the scrollbar and prevents scrolli ...

Are CSS designers setting boundaries for their designs by restricting themselves to layouts that can be handled by CSS from the start?

After seeking guidance on reaching CSS zen in this forum, I have come to realize that my struggles largely stem from positioning. Some sources suggest that CSS may not always be the most efficient layout system. As CSS designers, do we limit ourselves fro ...

What is the best way to ensure bootstrap cards' container expands horizontally?

Currently experimenting with Bootstrap 4 cards (view more at ) My goal is to have a container with a card deck inside that stretches horizontally as I add new cards, causing a horizontal scrollbar to appear when needed. By default, when the container wid ...

Purge the cache of CSS and JavaScript in your browser

Is there a way to clear JavaScript and CSS cache in ASP MVC without having to clear browser history? ...

Resolving CSS Fluid Image Problem

Even though my site background is responsive and functions well, I am encountering problems with the images. I want them to appear "fixed" in the same position as the background, regardless of resolution changes. For example, resizing the browser from 990p ...

Is there a way to ensure an element dynamically resizes to fill the remaining space completely?

I have a grid layout with 3 sections. My goal is to make the middle "map" section resizable so that when it's shrunk, the "points" section expands to fill up the remaining space. #grid { display: grid; grid-template-rows: 1fr 6fr 2fr; gr ...

PNG file unable to display in Google Chrome

I created an image using paint.net and saved it as a .png file. However, when I try to display the image on my website, only the borders show up without any image content. Here is the code I used: HTML <a href="home.php"><img id="logo" src="../i ...

The color of the links in the Bootstrap navbar refuses to budge

I'm having trouble changing the color of my navbar links to white and spacing them out evenly. I've written some code to do this, but for some reason, it keeps getting overridden. The background of the navbar is an image, and when I remove the im ...

What is the process for accessing an image via localhost on the same computer?

I created a program that saves images locally and stores their URLs in a database. When retrieving this data via ajax, the URLs are fetched and appended to a div class using the following code: $( document ).ready(function() { $.ajax({ url: "/ ...

Displaying information collected from a submission form

I am in the process of designing a cheerful birthday card and I need to transfer data from a form to the birthday card page. How can I take information from the first div (which contains the form) and display that data in the second div? <!DOCTYPE ...

Adjust the text color within an input field or a text area

I'm looking to customize the text color inside an input field, specifically making it white. Here is a reference image to demonstrate what I mean: https://i.sstatic.net/JJomd.png Below is the code snippet that achieves this effect: <v- ...

How can I use absolute positioning and scrolling with an Iframe?

One of the key elements of this website is the implementation of iframes, with only one displayed at a time. However, my current issue revolves around the inability to scroll within these iframes due to their absolute positioning. I have attempted variou ...

What is the importance of ensuring that user registration is done consistently?

While working on a user registration feature, I noticed that the character restrictions for the 'username' field in some sample codes are quite limited, only allowing '.', ''', and '-'. No spaces or other specia ...

Showing two hyperlinks in distinct columns within a table

I am encountering an issue with my system that showcases video tutorials. The code I have currently displays the video name (eNISATQuestion) and the video link (eNISATVideo) within one href tag. My goal is to include another column that will display a lin ...

Create a state model template solution by utilizing the powers of HTML, CSS, and JavaScript

Create a customizable state model diagram template utilizing HTML, CSS, and JavaScript. This solution should dynamically highlight the current workflow state of a specific customer's product. We have opted to utilize GoJS for this project, as it is l ...

Choose a row at random from the table, then delete it and use it to create a new table

I stumbled upon some code that really helped me out. Big shoutout to daiscog for that! I'll explain what I'm trying to achieve. There's a table with rows of entries for a giveaway, and I want to be able to click a button to remove a row from ...

How can you force a line break on an inline-block element with CSS, after a specific number of elements?

I'm struggling to arrange a list of divs in a grid layout using only CSS, but I can't seem to make it work properly. I've explored using white-space: wrap, but that's supposed to be applied to the parent element, so it doesn't solv ...