Can a fixed position element disregard its parent?

I've been experimenting with a div that switches between position: absolute and position:fixed on scroll. You can view the issue with my code on JSFiddle: http://jsfiddle.net/g9NVj/2/

The problem areas are the pink and blue boxes that change color as you scroll. The correct width and position (the desired result) is seen in the pink box, while the blue box is malfunctioning. The blue box with class .fix switches to position:fixed and sticks to the very left of the window, getting a different width (partially hidden behind a sidebar menu) instead of staying within its parent element.

I attempted to remedy the issue by adding position:relative to the parent element, but that did not resolve the problem.

Here is a snippet of the crucial part of the code from the fiddle, but please refer to JSFiddle for the complete code:

.content {
    width: 100%;
    overflow: hidden;
    position: relative;
    border-bottom: 1px solid #CCD5D5;
}

.c-2 {
    width: 50%;
    min-height:1px;
    float:left;
    overflow: hidden;
    /*position: relative;  TRIED THIS BUT DOESN"T WORK */
}

.c-2.last {
    padding: 80px;
}

.sticky {
    z-index: -1;
    background: #e4f;
}

.fix {
    position:fixed;
    width:50%;
    top: 0;
    left: 0;
    background: #34e;
}

.abs {
    position:absolute;
    bottom:0;
    width:50%;
    left:0;
    background: #e4f;
}

Answer №1

The reason for this behavior is that when using position:fixed, the element is positioned relative to the browser window, so applying position:relative to its parent div will not have any effect. To make the .fix div relative to its parent div, you need to give it a position:absolute and the parent div a position:relative.

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 challenge of navigating through $scope

In my Angular view/form, I have an input file element that is being utilized with ng-upload. The code snippet looks like this: <input id="img" type="file" name="image" onchange="angular.element(this).scope().setFile(this)"> <input id="imgname" ty ...

Ways to identify when an HTMLElement's size changes due to a percentage-based width setting

Currently, I am in the process of developing an Angular Component and have implemented the following CSS code: :host { display: block; width: 100%; } The main objective here is to ensure that the component remains as responsive as possible. However, ...

Tips on how to break free from a chain or leap over it using jQuery

Is it possible to skip or break out of a chain in jQuery? For instance: $("h3").click(function(){ //doSomething if(~~) // in this case, break out of the chain (functions A and B will not work) else if(~~) // in this case, j ...

Extracting the content within an HTML element through parsing

I am looking to extract the content inside these two elements and store it in a string: source_code = """<span class="UserName"><a href="#">Martin Elias</a></span>""" >>> text 'Martin Elias' Could someone guide ...

Challenges Arising from Using Django ORM with AJAX

Hey there! I need some help with Django ORM and ajax requests related to the select_related function. Let me explain what I'm trying to do. I have a query like this: prod_serv = Product_service.objects.select_related() This query involves joining 3 ...

Confused about having to use window.variableName in my code and not understanding the reason

Working on a web app with JS, Angular, and Meteor that integrates the Youtube API has been quite challenging. In one of my controllers, I initialized the youtube player object in the constructor following the necessary steps outlined by the Youtube API. Ho ...

`Look up values from specified key names`

Consider the following JSON data: const information = { "item1":1, "item2":20, "item3":123, "item4":[{"a":"apple","b":"ball"}], "item5":1211 } In ...

Utilizing Mongoose Schema for CSV Import

My current task involves importing a large CSV file into a mongo DB, where the order of the values will determine the key for each entry in the database: Here is an example of the CSV file structure: 9,1557,358,286,Mutantville,4368,2358026,,M,0,0,0,1,0 9 ...

Scope isolation prevents variables from being accessed in higher levels of the scope chain

In my search for answers, I came across some similar questions on SO: Isolate scope variable is undefined unable to access rootscope var in directive scope However, my question presents a unique scenario. I am facing an issue with a directive that has a ...

Updating the TextField in React Material UI based on the Slider value without affecting the Slider

Is there a way to update the value of a TextField based on the slider, while still allowing manual input in the TextField? I want the TextField to reflect changes from the slider but not vice versa. I attempted making the TextField a controlled component ...

Conceal the Div containing the specified class

I was hoping to hide the first DIV if the second DIV is displayed on the front end, and vice versa upon page load. If the first DIV is set to 'block,' then the second DIV should be set to 'none.' And If the second DIV is set to &apos ...

Creating interactive tables in HTML and Javascript where users can expand and collapse rows by clicking on a parent row

After days of trying to solve a specific issue, I have realized that I cannot do it without some help. The task at hand is to enable the functionality of clicking on a table row to reveal more details, but in my case, these additional details are presented ...

Adjust the prop value whenever there is a modification in the Vuex store

Following a successful mutation to the vuex store (state.posts.post.comments) with the use of this code snippet and implementing Vue.set for Vue to acknowledge the addition of an object property: store/modules/post.js const mutations = { [types.SET_P ...

Deactivate any days occurring prior to or following the specified dates

I need assistance restricting the user to choose dates within a specific range using react day picker. Dates outside this range should be disabled to prevent selection. Below is my DateRange component that receives date values as strings like 2022-07-15 th ...

Exploring the Sidebar Navigation Features of Bootstrap 4

Looking to incorporate a vertical navigation menu on the left side of my webpage, extending the full height of the page. My attempt to achieve this using the Bootstrap grid system resulted in the navigation menu becoming too wide. This issue persisted eve ...

Express middleware for handling errors with Node.js router

My application structure is laid out as follows: - app.js - routes ---- index.js The ExpressJS app sets up error handlers for both development and production environments. Here's a snippet from the app.js file: app.use('/', routes); // ro ...

Decoding a JSON object in node.js

{"__v":0,"_id":{"$oid":"55f13d34258687e0bb9e4385"},"admin":true,"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d6ded2c3dfd682f3d4ded2dadf9dd0dcde">[email protected]</a>","last_login":"11:25:24 AM","name ...

Using AJAX to load dynamically generated DIV IDs

On my website, I have a "command center" bar featuring a log in / log out button, a my account button, and a messages button. When the user is not logged in, only the login button is displayed. However, once the user logs in, the command center shows the l ...

How to delete URL parameters in JavaScript and HTML5

I have a URL that looks like this: /users/?i=0&p=90 How can I remove the part from ? to 90 Can anyone provide me with some JavaScript code to achieve this? EDIT I mean doing this with window.location.href directly in the browser URL bar. I trie ...

Retrieving specific values for each key in JSON using PHP

Looking for a solution with PHP to extract specific information from JSON files based on set criteria. Here is an example of part of the JSON data: "Main": [{ "count": 7, "coordinates": [89,77], "description": "Office", },{ "count": 8, ...