Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fixed VS absolute positioning for scrolling

I was just playing around with some CSS absolute and fixed properties and came across a unusual difference between absolute and relative positioning in CSS.

Basically, when I absolutely position something and the content is more than the height of the window or containing element, the scroll bar appears, but when I change the position to fixed, even though the content is more in height compared to the window, no scroll bars appear.

I have created a test case for this:

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;
}

and here's the fiddle: fiddle. (check the comment in the CSS)

Would appreciate an explanation on this issue.

like image 361
Alexander Solonik Avatar asked Mar 07 '15 14:03

Alexander Solonik


People also ask

How do you scroll with an absolute position?

You need to wrap the text in a div element and include the absolutely positioned element inside of it. Setting the inner div's position to relative makes the absolutely position elements inside of it base their position and height on it rather than on the . container div, which has a fixed height.

What is the difference between fixed positioning and absolute positioning?

Absolutely positioned elements are positioned with respect to a containing block, which is the nearest postioned ancestor. If there is no positioned ancestor, the viewport will be the containing block. Elements with fixed positioning are fixed with respect to the viewport—the viewport is always their containing block.

Which position property remain unaffected while scrolling?

fixed : the element is removed from the flow of the document like absolutely positioned elements. In fact they behave almost the same, only fixed positioned elements are always relative to the document, not any particular parent, and are unaffected by scrolling.

Should I use absolute or relative positioning?

As a special, use “relative” positioning with no displacement (just setting position: relative ) to make an element a frame of reference, so that you can use “absolute” positioning for elements that are inside it (in markup).


1 Answers

Fixed positioning means that the element is fixed in the viewport - it is not affected by scrolling. You can read more here: https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/

Since nothing else gives height to your page, then all you see in this case is whatever part of the fixed element fits in your viewport.

See what happens when you set a height to the container in this version: http://jsfiddle.net/93ubza81/3/

.page-content{
height: 3000px;
}

You have scrolling, but the fixed element isn't affected.

like image 175
Eyal Avatar answered Oct 16 '22 02:10

Eyal