Is the fixed div not behaving as expected inside a parent with relative positioning?

Seeking help to troubleshoot an issue I'm facing - I have two divs positioned relatively, with child elements positioned fixed within them. As I scroll the page, these two divs are supposed to become fixed to the top of the browser using the following code snippet:

//Code to make sub-nav and cart scroll with the page
$(document).ready(function() {
  var stickySidebar = $('.sideSubNav, .cartcontainer').offset().top;

  $(window).scroll(function() {
    if ($(window).scrollTop() > stickySidebar) {
      $('.sideSubNav, .cartcontainer').addClass('affix');
    } else {
      $('.sideSubNav, .cartcontainer').removeClass('affix');
    }
  });
});

.sideSubNav.affix,
.cartcontainer.affix {
  position: fixed;
  top: 0;
}

Previous to being fixed, the width of the divs matches that of the containing div. However, once fixed, the width shrinks. I am attempting to maintain the same width when fixed, but adding width: 100% causes it to span the entire page, disregarding the container. Fixed positioning seems to ignore the container's dimensions, unlike absolute positioning which respects them. Is there a workaround to ensure that fixed positioned divs respect their container? Keep in mind that I'm working with a responsive layout in Bootstrap 4.

Answer №1

If an element has the CSS property position: fixed;, it will be placed in relation to the viewport, not its closest ancestor with position: relative or position: absolute.

To position child elements, use position: absolute, and for the parent element, use position: relative as the reference point.

For more information on CSS positioning techniques, check out this informative guide HERE.

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

Increasing the opacity of a background image when a new div is added

While working on my chatbot assignment, I set the background image opacity to 0.02. Unexpectedly, as I input more messages, the opacity gradually increases all the way to 1. Although this effect is pretty neat, it wasn't my original intention. Some ...

Alignment issue with React JSX background position; image not centered on the right in Material UI Grid item

I've recently completed a single-page website portfolio, but I'm encountering an issue with setting the background position of the home screen to center right. You can view my site here: Here is the link to my repository: https://github.com/Bolm ...

How to change the value of an input dynamically using jQuery?

Looking for a solution where the 3 text input areas in a single form can have their values loaded dynamically from an INI file, such as "blue", "red", "green". The challenge is to implement this without having to manually input the values or refresh the ...

Unlock the secrets of creating a pop-up with qtip

I am working on qtip code that generates a popup when a specific link is clicked. I'm wondering if there's a way to use jQuery to determine if the link with the id "test2" has been clicked or not. Thank you, James <ul> <li><a id= ...

How can you utilize CSS to automatically generate section numbers, specifically for multiple sections?

Is it possible to automatically generate section numbering in CSS only when there are multiple sections present? I discovered that using CSS, one can create automatic numbering for sections. body { counter-reset: section } h2 { counter-reset: sub-section ...

AJAX list refresh, fetch additional items and tally

Looking for a solution to update the values of listUsernames and numUsernames after adding an item? Check out this scenario: <ul id='usernameList'> <li class='username'>John</li> <li class='username&apo ...

Interactive jQuery Popup character map

Is there a jQuery plugin available for a popup 'character map'? I have a text field and want users to easily insert special characters, like the TinyMCE plugin. ...

Attempting to showcase two lines within a single row on an html page

Currently, I am working on a project involving ASP MVC and Bootstrap where I need to display the regulation of a transformer in a specific format. This representation can be seen in the image below: https://i.sstatic.net/VzVcA.png My attempt to achieve t ...

The error message encountered was: "jQuery has not been defined."

I'm struggling to understand why I'm encountering this issue. Take a look at the code snippet below: <head> <script src="https://code.jquery.com/color/jquery.color-2.1.2.min.js" type="text/javascript"></script> ...

Stop the stutter in Chrome caused by scrolling animations

I'm encountering a delay with the scroll.Top() function. This issue is specific to Google Chrome on Desktop. Here is the jQuery code I am using: $('html, body').animate({ scrollTop: $('#second').offset().top-140 }, 'slow&apos ...

Using Laravel and Bootstrap v4, you can easily implement a feature that redirects to a specific tab after

I have a webpage featuring 3 tabs. I am trying to set up the page so that it redirects to the same tab that was active before the reload. I assume each tab should append a string to the URL, like #menu1 or #menu2, but I'm having trouble implementing t ...

Troubleshooting a touchend event problem with jQuery in a Cordova environment

Initially, I utilized a listener for mousedown and mouseup events to handle interactions from one canvas to another on the website. However, when transitioning to mobile compatibility, I switched to using touchstart and touchend events. Unfortunately, the ...

What is the best way to center my navigation bar without interfering with the mobile version's JavaScript functionality?

Just starting out with web development and stack overflow, so please bear with me if I struggle to explain the issue. I came across some JavaScript to make my website responsive on small screens (mobiles). However, I am having trouble centering my top nav ...

attach jQuery extension to several components

After spending hours scouring the internet, I have yet to find a solution to my issue. The problem is that I have developed a plugin for creating a typing effect on any given element, and it works perfectly when applied to a single element. However, when I ...

Display or conceal a div when hovering over dropdown menu choices

$('#dropdown-list').on('change',function(){ if( $(this).val()==="option 1" ){ $("#diva").hide() } else { $("#divb").show() } }); The code above is being used to hide or show a div based on a dropdown selection, which is f ...

Ways to forward from an ajax-triggered action

I am looking to immediately redirect from the "Login" action to the "Home" action without going through the ajax success callback. Currently, when the "Login" action is triggered via ajax, it redirects to the "Home" action, but the ajax success callback st ...

Modifying line height with Jquery

Looking for help with my html code. I have a <div id="listview"> element with an unordered list inside containing a list item that has its height dynamically set using inline CSS. I'm trying to change the height of the list item using jQuery, bu ...

Exploring the mechanics of JavaScript and jQuery Ajax: What's the secret to its function

It's common knowledge that browsers are single-threaded, meaning they can only either render the UI or execute Javascript snippets. While web workers offer a way to achieve multi-threading, let's focus on the default behavior. I'm curious a ...

Adding two unique identifiers together using Ajax can be easily accomplished by sending a request

I have implemented an Ajax function to display auto suggestions in a search field. However, I have two search fields on the same page and when I try to use the function, it only works for one of the fields. This is because both fields have the same ID and ...

Utilizing jQuery to dynamically update dropdown menus through AJAX requests

I am working on implementing a radio button to change a dropdown menu and I have most of it working, but I am unsure about a few elements. My goal is to use CreativeID as the ID and CreativeName as the name. Below is the AJAX code snippet I am using: $(&a ...