Web development and Tech news Blog site. WEBISFREE.com

HOME > css

Resolving the issue of using "position: fixed" CSS property with a parent element that has "position: relative"

Last Modified : 24 Feb, 2023 / Created : 24 Feb, 2023
596
View Count
When using the "fixed" value for the "position" CSS property, the element is aligned with respect to the root area of the web document (viewport), rather than its parent element. However, there is an exceptional case where the element may not appear as expected. In such cases, how can we resolve the issue?




! position: fixed; how can we resolve the issue?


The only issue that arises in such cases is that the element should be fixed with respect to the entire document but appears according to the position set for its parent. Although we have used the "fixed" value, the desired result may not be achieved. Moreover, when scrolling, the fixed element may move in such cases. The reason for this issue lies in the style properties of the parent element.


If the parent element has any of the three style properties - "transform", "filter", or "perspective" - then the child element with "position: fixed" will not work as expected, and will become relative to the parent like "absolute".

Note that the parent element does not necessarily refer only to the immediate parent element, but it may also inherit the style properties from its parent or even further up in the hierarchy.

For example, two elements A and B may have the same style, but one may have a parent element with the "filter" property, causing them to behave differently, which can be observed when scrolling.

<div class="flex">
<div class="test test-1">
<div class="test-1-1">No parent element with transform, filter, or perspective properties</div>
</div>
<div class="test test-2">
<div class="test-2-1">Parent element with transform property</div>
</div>
</div>
<style>
.test { width: 300px; height: 300px; border: 1px solid #000; color: #fff; }
.test-2 { transform: rotate(0); margin-left: 10px; }
.test-1-1 { width: 150px; height: 150px; background: black; position: fixed; top: 40%; left: 40%; }
.test-2-1 { width: 150px; height: 150px; background: black; position: fixed; top: 40%; left: 40%; }
</style>


The above example clearly demonstrates how two elements with the same style can appear differently. For reference, the applied code is as follows. First, the HTML code is:
<div class="test test-1">
  <div class="test-1-1">Parent element without transform, filter, perspective</div>
</div>
<div class="test test-2">
  <div class="test-2-1">With transform</div>
</div>

Next, here is the style:
.test {
  width: 300px;
  height: 300px;
}
.test-2 {
  transform: rotate(0); // Only test-2 applied transform
}
.test-1-1 {
  position: fixed;
  top: 40%;
  left: 40%;
}
.test-2-1 {
   position: fixed;
  top: 40%;
  left: 40%;
}


If, when using fixed, it does not behave as expected, it is necessary to check if any of the parent elements use the three properties mentioned above: perspective, transform, or filter. If any parent element uses these properties, in order to resolve the issue, it may be necessary to use a different layer instead of the affected one, or to replace the parent's style in another way.

Although this is a simple issue, it can be difficult to identify the cause when the same issue arises, so it can be very useful to keep in mind.
Perhaps you're looking for the following text as well?

Previous

[CSS] Learn the CSS property clip-path. Circles, ellipses, polygons

Previous

Preprocessor Sass/SCSS to CSS compilation transformation