The fixed position div remains stationary as the page scrolls

Below, I am attempting to fix the position of the 'inner-navigation' div within the 'compare-display' box by making it absolutely positioned. However, when scrolling, the 'inner-navigation' div is not staying fixed. How can I solve this issue?

View my fiddle here:

http://jsfiddle.net/Cd9eZ/

HTML Code

<div class="compare-display">
    <div class="table">
        <div class="source-compare col-50">
            <div class="page"></div>
        </div>
        <div class="navigation-compare">
            <div class="inner-navigation"></div>
        </div>
        <div class="target-compare col-50">
            <div class="page"></div>
        </div>
    </div>
</div>

CSS Code

.table {
    display: table;
    height: 100%;
    width: 100%;
}
.table > div {
    display: table-cell;
    vertical-align: top;
}
.table > .col-50 {
    width: 50%;
    background: green;
}
.compare-display {
    position: relative;
    overflow: auto;
    height: 200px;
}
.compare-display .navigation-compare {
    min-width: 50px;
    background: blue;

}
.compare-display .page {
    margin: 20px;
    height: 500px;
    background: orange;
}
.compare-display .inner-navigation {
    position: absolute;
    width: 50px;
    top: 0;
    bottom: 0;
    background: red;
}

Answer №2

To create a fixed div around '.compare-display' and eliminate the relative positioning of '.compare-display', follow these steps:

Check out this example:

jsfiddle.net/Cd9eZ/4

.compare-wrapper {
    position:fixed;
}

.compare-display {
    /*position: relative;*/
}

<div class="compare-wrapper">
    <div class="compare-display">...</div>
</div>

Answer №3

Were you aiming for this result?

http://jsfiddle.net/Cd9eZ/3/

 .compare-display .inner-navigation {
   **position: fixed;**
   width: 50px;
   top: 0;
   bottom: 0;
   background: red;
 }

If you wish to have the navigation affixed to the window, using absolute positioning will not achieve the desired effect. Absolute positioning does not remove the element from the page flow, so scrolling will always affect an absolutely positioned element as the reference point is the page, not the window.

Fixed elements, on the other hand, are completely removed from the page flow and are attached to the viewport itself.

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

Unable to access Bootstrap nav link

There seems to be an issue with a link inside the Bootstrap nav-menu. The link is not opening when clicked on. I am facing trouble with the Cart file as it does not open when I click on the Cart hyperlink. I have double-checked the path and it appears to ...

Rearranging div placement based on the width of the screen

I am currently working on building a responsive website and I need two divs to switch positions depending on the screen width, both on initial load and when resizing. Despite my efforts in researching and trying various options, I have not been successful ...

The Challenges of CSS Specificity in Multiple Browsers

I am using this as an opportunity to learn, so I appreciate if you can refrain from telling me what not to do and instead help me understand the concepts. What I am trying to achieve here is comprehension. Here is a sample of CSS code: input[type="file"] ...

Create a hover effect on HTML map area using CSS

Is it possible to change the background color of an image map area on hover and click without using any third-party plugins? I attempted the following code: $(document).on('click', '.states', function(){ $(this).css("backgro ...

What is the reason for the ul selector not functioning in the attached CSS file?

I attempted to create a navigation bar by using the code below. <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="styles.css"> </head> <body class="background"> <ul> <li>Home</li> <li>Ne ...

Looking to screen usernames that allow for the use of the DOT (.), underscore (_), and Dash (-)?

I am using jQuery to filter usernames that are exactly 3 characters long. The validation criteria includes only allowing the following special characters: ., _, and - var filter = /^[a-zA-Z0-9]+$/; Therefore: if (filter.test(currentval)) { //valid ...

Pause until the existence of document.body is confirmed

Recently, I developed a Chrome extension that runs before the page fully loads by setting the attribute "run_at": "document_start". One issue I encountered is that I need to insert a div tag into the body of the webpage as soon as it is created. However, a ...

The level of transparency linked with text in a blockquote

I've spent hours on this and I'm completely lost. Even though I believe my code is correct, it keeps telling me it's wrong. I don't know where to go from here. The task requires adding a style rule for the blockquote element that chan ...

Why is there an excessive amount of white space on my Reactjs page?

This webpage (page URL -) displays well without any white space between two images when viewed on a laptop. However, when accessed on a mobile device, it appears to be taking up too much space as shown in the image below. Image of webpage on laptop- lapto ...

Is it possible to generate excel spreadsheets using a selection in Laravel?

In my system, I have implemented a radio group as a filter for my table which displays items as active, inactive, or both. Additionally, there is a button that allows users to export the table data into an Excel document. Currently, the export button downl ...

AmazingStew 4 - How to substitute anchor element with its text

My document contains HTML content and I am looking to replace all anchor tags with their respective texts using BeautifulSoup. Here is the input: html = '''They are also much more fuel-efficient than <a href="http://someurl.com">rock ...

Is it possible to combine ng-switch and ng-if directives in Angular?

I attempted to combine the angular ng-switch and ng-if directives, but it doesn't seem to be functioning properly. Here is what I tried: <div data-ng-if = "x === 'someValue'" data-ng-switch on = "booleanValue"> <div data-ng-swit ...

I am struggling to find the right way to center those images

Hello everyone, I'm currently attempting to set up the main content of the homepage resembling the image provided, but I'm encountering some challenges. Every approach I take seems to cause the image to overflow the container and occupy the enti ...

How can I fix the issue where Sx is not converting the styles to CSS format for the fill property exclusively?

The primary-main is supposed to change color to something like #abcdefg, but it seems to be stuck displaying primary-main as is. I am working with angular MUI + React ...

Scrolling to an id within dynamically loaded content using jQuery after an ajax request

After loading content via ajax, I am unable to scroll to the item, as the page remains at the top. Strangely, when I refresh the page dynamically with content caching enabled, the page scrolls to the desired target. var startPageLoader = function( pid, si ...

The placement of buttons in a responsive web design

Need help with adjusting button positions in mobile mode relative to text. Tried using different position settings like absolute and relative, but buttons are not staying aligned with the text on mobile devices. Can anyone assist? .button { display: b ...

Filling a select element in an HTML file using AJAX

I've hit a roadblock, trying to display this PHP list. <?php include 'connection.php'; $query = mysqli_query($conn, "SELECT * FROM expense_object ORDER BY code" ); ?> <!DOCTYPE html> <html lang=" ...

Trouble with triggering HTML5 FileReader() functions

I'm having trouble determining why neither readSuccess() nor readFailure() are being executed in the code snippet below: function readMyFile(){ var reader = new FileReader(); reader.onload = readSuccess; reader.onerror = readFailure; ...

Adjusting the position of the parent div by manipulating the bottom property

Is there a way to toggle the bottom property of a div when clicking its child anchor tag? <div class="nav" :class="{ 'full-width': isFullWidth }"> <a class="toggle-button" @click="toggleNav"><i class="fa fa-angle-down">< ...

How can I showcase both a username and email address in a Material UI chip?

I'm currently using Material UI chip to show name and email next to each other. However, when the name is long, the email goes beyond the chip boundary. Here's my function that generates the chips: getGuestList() { let {guests} = this.sta ...