Relative position of an element with position: fixed

Relative position of an element with position: fixed

Posición relativa de un elemento con position:fixed

On many webs we often see the use of ‘position:fixed’ to keep an element visible the whole time in spite of the page scrolling. For example, we can see that effect on Privalia’s web, where the navigation menu is always in sight. The problem with ‘position:fixed’ is that most times it needs JavaScript help to get the effect we generally wish. Regarding Privalia, the element with ‘position:fixed’ is kept fixed to viewport but in relation to a parent element in order not to cover up the header or the footer, something which has to be done with JavaScript.

It is because of this that I think it would be interesting to propose a change in the specification, so that ‘position:fixed’ gains position and dimensions relating to the immediate parent element with ‘position:relative’, keeping its position within the viewport area.

In the next example, we see the normal flow of ‘position:fixed’ without the JavaScript support, in which the element with ‘position:fixed’ is always in the same position with respect to the viewport when moving the example’s scrolls. In this example the relevant part of the code is only the CSS of the fixed element.

The two problems with this CSS, with respect to the lay out problem, is that as much the top position as the width dimension are relating to the viewport and not to a parent element, that is why we can not get an effect of a fixed element with respect to the parent element. The element is always in the same position with respect to the viewport and we cannot interact more with it.
[/vc_column_text]

[iframe: src=”/contenidos/position-fixed/position-fixed.html” width=”100%” height=”300″]

In the next example, we have used JavaScript to get the wished effect on the element with ‘position:fixed’. The element is kept visible in the viewport but within the area of the parent element. So that when the parent element gets out of the viewport, the element with ‘position:fixed’ also goes out together with it. We can compare the behavior of both examples by moving the vertical and horizontal scroll bars. In many cases we usually forget the horizontal scroll bar, as in the case of Privalia. Besides the position, the width of the element with ‘position:fixed’ has to be taken into account when the parent has a percentage width.

[/vc_column][/vc_row]

Tags:
©2019 PoseLab SL. All rights reserved.