OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Position absolute vs fixed on mobile device

  • Thread starter Thread starter Fishlex
  • Start date Start date
F

Fishlex

Guest
I Have the following HTML snippet:


Code:
.parent {
  height: 100vh;
  position: relative;
  background: MediumSeaGreen;
}

.child {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: white;
  color: black;
}

Code:
<div class="parent">
  <div class="child">
    Hello World!
  </div>
</div>

If I use position: absolute on the child I get a vertical scroll while seeing the page on a mobile device. If I change that position to position: fixed I don't have the scroll.

I am trying to understand why the scroll appears with position absolute and why it doesn't with position fixed. From my understanding position absolute is relative to the nearest parent that have position: relative. In my case that's the first div which occupies the full screen.

Shouldn't position absolute be contained in the parent div and the scroll be prevented? In other words shouldn't the height of the page be maintained at 100vh since position: absolute is removed from the flow of the page?

Note: I get the scroll on Chrome and Safari. With Firefox it works as expected on a mobile device. I tested with an IOS device and an Android one.
<p>I Have the following HTML snippet:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-override"><code>.parent {
height: 100vh;
position: relative;
background: MediumSeaGreen;
}

.child {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
background-color: white;
color: black;
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="parent">
<div class="child">
Hello World!
</div>
</div></code></pre>
</div>
</div>
</p>
<p>If I use <em>position: absolute</em> on the child I get a vertical scroll while seeing the page on a mobile device. If I change that position to <em>position: fixed</em> I don't have the scroll.</p>
<p>I am trying to understand why the scroll appears with position absolute and why it doesn't with position fixed. From my understanding position absolute is relative to the nearest parent that have <em>position: relative</em>. In my case that's the first div which occupies the full screen.</p>
<p>Shouldn't position absolute be contained in the parent div and the scroll be prevented? In other words shouldn't the height of the page be maintained at 100vh since <em>position: absolute</em> is removed from the flow of the page?</p>
<p>Note: I get the scroll on Chrome and Safari. With Firefox it works as expected on a mobile device. I tested with an IOS device and an Android one.</p>
Continue reading...
 

Latest posts

Z
Replies
0
Views
1
zuegelwagen
Z
Top