Home  >  Article  >  Web Front-end  >  Why Does `position: sticky` with `bottom: 0` Behave Opposite to MDN\'s Description?

Why Does `position: sticky` with `bottom: 0` Behave Opposite to MDN\'s Description?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-21 19:38:13355browse

Why Does `position: sticky` with `bottom: 0` Behave Opposite to MDN's Description?

Opposite Behavior of position: sticky with bottom: 0

When specifying position: sticky with bottom: 0, it behaves differently from the definition provided by MDN. Unlike the description, where elements are initially treated as relatively positioned until they exceed a threshold and become fixed, the opposite occurs with bottom: 0 specified.

In the provided code example:

<footer>
  <div class="footer"></div>
</footer>
footer {
  position: sticky;
  bottom: 0;
}

MDN states that the footer element should first be relatively positioned until it moves less than 0px from the bottom of the viewport. However, the observed behavior is the reverse:

  • When scrolling down: The footer starts as fixed positioned, then becomes relatively positioned when the bottom of the footer reaches 0px from the bottom of the viewport.
  • When scrolling up: The footer starts as relatively positioned, then becomes fixed positioned when the bottom of the footer moves past 0px from the bottom of the viewport.

Reason for the Opposite Behavior

The key to understanding this behavior lies in the wording of the MDN definition: "treated as relative position elements until the specified threshold is exceeded."

This means that the initial position is determined by the HTML structure and the distance to the specified threshold. In the case of bottom: 0, the footer is already at the bottom of the viewport when the page loads. Thus, it starts in a "fixed" state since the threshold (0px from the bottom) is already exceeded.

Conclusion

Therefore, when specifying position: sticky with bottom: 0, the element will initially be fixed positioned and transition to a relative position when the element moves past the bottom of the viewport. This behavior is opposite to the described behavior in the MDN documentation due to the initial position being determined by the HTML structure and proximity to the specified threshold.

The above is the detailed content of Why Does `position: sticky` with `bottom: 0` Behave Opposite to MDN\'s Description?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn