Comparison between fixed and absolute positioning when scrolling

During my CSS exploration, I discovered a peculiar contrast between absolute and fixed positioning in CSS.

Essentially, when I apply absolute positioning to an element with content exceeding the height of the window or containing element, scroll bars appear. However, when I switch to fixed positioning, no scroll bars are displayed despite the excess content height.

To demonstrate this difference, I've created a test case:

HTML:

<div class="page-container">
    <div class="helper">
        <div class="marker red"></div>
        <div class="marker green"></div>
        <div class="marker yellow"></div>
        <div class="marker blue"></div>
    </div>
</div>

CSS:

#slides-container {
    height: 100%;
    width: 100%;
    overflow: hidden;
}
.helper {
    height: 400%;
    width: 20px;
    position: fixed;   /* change this to absolute and the scrollbars appear*/
    top: 0;
    left: 0;
}
.helper .marker {
    height: 25%;
    width: 100%;
}
.marker.red {
    background: red;
}
.marker.green {
    background: green;
}
.marker.yellow {
    background: yellow;
}
.marker.blue {
    background: blue;
}

Here's the fiddle link for reference: fiddle. (Make sure to check the comment in the CSS)

I'm eager to learn more about this issue and would appreciate any insights or explanations.

Answer №1

When an element is fixed in the viewport, it remains stationary and does not move when the user scrolls. For more information on fixed positioning, check out this link: https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/

In cases where nothing else contributes to the height of your page, only the portion of the fixed element that fits within the viewport will be visible.

To see the impact of setting a height for the container, take a look at this version: http://jsfiddle.net/93ubza81/3/

.page-content{
height: 3000px;
}

Even with scrolling enabled, the fixed element remains unaffected by the movement.

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

An obstacle with Google fonts in NextJs - Only a single font is appearing

In my NextJs application, I've added a Google Fonts link to the HEAD section of the _app.js file. This link includes two different fonts. https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;700&family=Poppins:wght@400;700&display=swap H ...

Including JavaScript in HTML Error 404

https://i.stack.imgur.com/aQDPG.png I am struggling to understand why this import is not functioning as expected. I have tried using script/import.js but it still fails to work. The error message I keep receiving is: 127.0.0.1 - - [09/Sep/2020 15:09:35] ...

Looking to showcase an uploaded image next to the upload button in Vue.js using Element.ui?

I am currently working on implementing a feature that allows users to preview an image after uploading it. My approach involves uploading the image and then making an axios/AJAX call. Please see the following code snippet for reference: HTML code: ...

Ways to incorporate vertical alignment while adjusting the size of a <div> block

Seeking advice on how to vertically align <div> blocks (green blocks in my example) when changing block size. What adjustments are needed in my example for the vertical alignment (middle) of side blocks to be maintained when resizing the browser win ...

Utilizing the toggle switch functionality in Django with a boolean field implementation

My work_experience model includes an "is_working" field which is set to true when a user is currently employed at a company. I am using a toggle switch on the front end and would like to update the boolean field value for "is_working" with a click. What lo ...

I am having issues displaying my background image

I'm experiencing an issue where my background image isn't displaying correctly. Here is the CSS code I'm using: body { background-image: url('img/bg.png'); background-repeat: no-repeat; } ...

Incorporate classes into multiple titles by utilizing the text within the title

Here is some HTML code: <tr class="more-options"> <td width="190px" valign="top" class="ms-formlabel"> <H3 class="ms-standardheader"> <nobr>Required Hidden 1&l ...

"Create a dynamic box grid with varying heights using the responsive design features of Bootstrap

I have been attempting to create a responsive grid consisting of 6 textboxes using the box class and bootstrap. My issue is that due to varying amounts of text in each textbox, they end up with different widths/heights. However, I want them all to have th ...

Obtain an Array Containing all Current Page Paths in Gatsby

While creating a custom `404` page in Gatsby, I am looking to provide users with recommended similar path names based on the non-existent path they have accessed. Can anyone guide me on how to retrieve an array of all the current pages on my website? ...

The disruptive puzzle of shuffling with jQuery

For this project, I have incorporated the jQuery shuffle plugin from this resource. Here's the scenario: Within a <div class="masterDiv"> element, there are multiple <span class="toBeShuffled"> elements. Each <span> element contains ...

Steps for creating a functional dropdown menu using list items and retrieving the selected values

I am looking to implement a feature with multiple drop-down menus using list items. The functionality I desire is that when the user clicks on a ul element, the list items should drop down. Then, upon clicking on a specific list item, the value of that ite ...

Hover Over - Display MySQL Information

Currently, my code looks like this: <?php $mysql_hostname = "localhost"; $mysql_user = "root"; $mysql_password = "password"; $mysql_database = "test_db"; $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops s ...

Problem with the visibility of Grid Layout

Trying to create a Quiz web application, I want the quiz container to be hidden with the CSS property display: none; until I click "start game." Once the game starts, the container should change to display: grid;. However, when I try to reveal the div, it ...

Various CSS regulations for various languages/modes in CodeMirror

Can CodeMirror highlight different languages separately in multi-mode? .cm-s-default .cm-string-html {color: blue;} .cm-s-default .cm-string {color: #170;} Usually, CodeMirror applies the same class names for strings, comments, and other objects in all l ...

Is there a way to adjust the paragraph structure within the SharePoint RichHtmlField feature?

When working with SharePoint, we utilize the richhtmlfield feature to enable users to edit the HTML content of a page. Is there a way to modify the CSS that is generated when they select headers like H1, H2, or use the Normal formatting option? Additional ...

Switching from portrait to landscape view on a mobile website

Currently, I am working on a mobile website where the navigation bar looks great in portrait mode with 15px of padding around the top and bottom. However, when I switch to landscape mode, it occupies almost half of the screen. Does anyone have suggestions ...

Words fail to display

.sport { content: url("../img/sport.png"); background-color: rgba(0,0,0,1.0); top: 61px; height: 310px; width: 300px; position: absolute; margin: 0; left: 0; border-radius: 4px; overflow: hidden; } .sportsandactivis { background-colo ...

sound not functioning on iPad

<audio id="example" controls="controls"> <source src="1.mp3" type="audio/mpeg" /> <source src="1.ogg" type="audio/ogg" /> </audio> JavaScript code to automatically play video when the page loads document.getElementById(&ap ...

downloading responsive design

Using media queries to define backgrounds with separate images based on browser size: @media (min-width:1440px){div.container{background:URL('bg1440.png');}} @media (min-width:960px){div.container{background:URL('bg960.png');}} @media ...

A viewless Angular 2 component

Is it feasible to utilize Angular 2 without the need for a template or an @View? I am exploring alternative methods akin to the example shown below: Angular 1 index.html <div ng-controller="appcontroller"> <div ng-class="{active: isActive()}"& ...