How to use absolute positioning in CSS within a scrollable container using Bootstrap 4

I'm attempting to create a scrollable div with absolute positioned children inside. My goal is to keep the absolute positioned elements visible even when the parent is scrolled. I am aware that I need to set the parent element to position: relative in order to achieve this, but I am using Bootstrap 4 and setting the parent to relative does not seem to make any difference.

You can check out the initial state here, where the parent is scrollable. Take note of the lines-scrollable class:

<div id="opened" class="col-12 align-self-start lines lines-scrollable">

Here you can see what happens when the overlay and popup (absolute positioned children) are shown. Notice the lines-non-scrollable class on their parent:

<div id="opened" class="col-12 align-self-start lines lines-non-scrollable">

Everything works perfectly up to this point because the scroll position is at 0:

$('#opened').scrollTop(0);

However, if you start scrolling, the absolute positioned children also move along with the scroll, going out of view. You can simulate scrolling by doing:

$('#opened').scrollTop(100);

Answer №1

By setting something as absolute, you are placing it in a position relative to the nearest container with position absolute or relative. However, when you scroll down, the 0 position shifts up, causing this behavior.

In simpler terms, as you scroll the div upwards, the relative location 0 also changes, resulting in the inner absolute div moving up as well.

You have two choices:

  1. To address this issue, you can set the position to fixed, which will make it relative to the page. For example, using position top: 0; will always keep it at the top of the page regardless of its placement in the HTML.

Alternatively,

  1. You can move the inner absolute div up one level and make the container relative. Your HTML structure would then resemble the following:

https://jsfiddle.net/3adctabu/6/ - Here is a working example based on the jsfiddle link provided for the second option.

<div class="outer-container-that-is-relative">
  <div class="inner-absolute"></div>
  <div class="scrollable-content"></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

Show a pop-up notification when the mouse passes over a word in the text

I've been grappling with this issue for days now and could really use some guidance. Despite scouring the web, I'm unsure if I've approached it correctly. What I'm trying to achieve is having an alert box pop up each time a user hovers ...

How can you make a form group expand within a container in Bootstrap 4?

After experimenting with the paddings, I still couldn't resolve the issue at hand. The goal is to extend these fields all the way to the right side. Check out the Fiddle for comparison between the current appearance and the desired look. The stylin ...

Element Style Does Not Align with Computed Z-Index

Currently, I am in the process of developing an HTML5 video player. However, I have encountered a problem where my custom controls become unclickable once the video element is set to fullscreen mode. This issue arises because the video's z-index is se ...

quickest method for attaching a click listener to dynamically added elements in the document object model

When adding multiple elements to the DOM, how can we ensure that these elements also have a click handler attached to them? For instance, if there is a click handler on all elements with the "canvas-section" class, and new "canvas-section" elements are con ...

When attempting to access the submenu in Bootstrap 5 dropdown, it does not slide down as expected upon clicking

Why isn't the rest of the menu expanding downward when I select a submenu? The submenu is already selected, but it hides other items. How can I make it expand downwards? <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

Load a variety of tiles to create a unique style and background for the

Currently, I am using this function to randomly retrieve an image. <script> window.onload = choosePic; var myPix = new Array( "https://ry3yr.github.io/OSTR/Diarykeepers_Homepage/extrafiles/images/bgfiles/cool_stuff_anim.gif", "https:// ...

Use HTML accept-charset to specify the character encoding for form fields

Curious if a form field could handle characters with accents like Ś or ý and display them as the raw letter (Ś or ý) instead of "random symbols" like percents upon submission. Is this achievable? ...

Learn how to display two different videos in a single HTML5 video player

Seeking a solution to play two different videos in one video element, I have found that only the first source plays. Is jQuery the answer for this problem? HTML Code: <video autoplay loop id="bbgVid"> <source src="style/mpVideos/mpv1.mp4" type ...

Custom font not displaying on Chromecast receiver app

I have followed the specified steps to incorporate a custom font into an html canvas text field. Interestingly, the font displays correctly when accessed on the Desktop Chrome browser, but on the Chromecast receiver application, the font fails to load. Wha ...

Harvest information using BeautifulSoup and Python

Exploring the world of web scraping with BeautifulSoup in Python, I am interested in extracting the package name and price of each app from the Android Play Store. For retrieving the package name, I implemented the following code : url = "https://play.go ...

Can a list be transformed into a toolbar in a responsive design?

Here is my card with a nav section that includes multiple items (although I've only included one here for simplicity). <div class="col-md-3"> <div class="card"> <div class="card-header"> ...

Content not being displayed by Javascript

I'm having trouble displaying content using JavaScript DOM. Every time I try to run my code, the page comes up blank. Here is an example of my HTML file: <!DOCTYPE html> <html> <head> <title>Radiant HQ</titl ...

Update the background's color by cycling through a set of colors stored in

I have a container on my webpage with a light blue background, and I want the background color to darken progressively with each click. Currently, I am manually setting an array of colors for this purpose, but I am struggling to make the background color c ...

Updating HTML using Angular JS with a nested dictionary is a breeze

As a newcomer to AngularJS, I am facing an issue with my code. I have created a dictionary in the controller scope and populated it after an HTTP request (key, value pair). The dictionary is being created successfully and there are no errors, but the HTML ...

Incorporating React State into CSS Styles

Currently, I am immersed in a project based on NextJS where I have to utilize React State to determine the width of a div. In the existing setup, this calculation takes place within a .tsx file using constants and is incremented upon clicking a button. The ...

Using a CSS button to activate a JavaScript function: A step-by-step guide

My current project involves writing a script to change the color of text when a specific button is clicked. The idea is that clicking the "Change color1" button triggers the text color change using the following code snippet: <button onclick="myFunction ...

Having trouble displaying an image in your React project?

I'm having an issue with my code where the logo is not displaying, even though it should be in the src -> pages -> images folder. I can't seem to figure out why this is happening. import dress from "./images/dress.png"; const I1 = () => ...

"Utilizing jQuery to present the accurate sequence within a table

https://i.sstatic.net/iCpnO.png I am currently experiencing an issue with my JavaScript code. I am trying to display a single column for "Success/Fail" in a table, but it is showing as two separate columns. The values for the "Success/Fail" column are not ...

Looking for a list that you can edit with CSS styling?

I'm struggling to create a flexible list that can be easily modified. My goal is to have an ordered list with unique labels such as 1,2,3,4,5,6,7,8,9,0,A,B,!,# Although I've managed to number the list items, I am stuck on adding the extra label ...

"Finding the Perfect Alignment: How to Center Legends in Firefox

The issue at hand An issue has been identified where the code functions as intended in Chrome, Opera, and IE but fails to work in Firefox: fieldset>legend { display: table; float: none; margin: 0 auto; } <fieldset> <legend>Legend ...