This is a question when I read an article on the MDN position
property. I thought that there was a clear difference between the behavior of sticky
described there and the actual behavior.
According to the MDN, sticky position elements are treated as relative position elements until the specified threshold is exceeded, and when the threshold is exceeded, they are treated as fixed position elements until the boundary of the parent element is reached (Link).
Sticky positioning can be thought of as a hybrid of relative and fixed positioning. A stickily positioned element is treated as relatively positioned until it crosses a specified threshold, at which point it is treated as fixed until it reaches the boundary of its parent. For instance...
#one { position: sticky; top: 10px; }
...would position the element with id one relatively until the viewport were scrolled such that the element would be less than 10 pixels from the top. Beyond that threshold, the element would be fixed to 10 pixels from the top.
So, I created the following code and confirmed the operation.
body {
margin: 0;
}
.container {
display: flex;
flex-direction: column;
}
.container>* {
width: 100%;
}
header {
background: #ffa;
height: 130vh;
}
main {
background: #faf;
height: 210vh;
}
footer {
background: #faa;
height: 8vh;
position: sticky;
bottom: 0;
}
.footer {
background: #aff;
height: 100vh;
}
<div class="container">
<header>HEADER</header>
<main>MAIN CONTENT</main>
<footer>FOOTER</footer>
<div class="footer"></div>
</div>
According to the MDN article, this code "is a relative placement element until the position of the element is less than 0px from the bottom of the viewport by scrolling the viewport, and becomes a fixed placement element when it is more than 0px from the bottom" I was thinking.
However, the result is the action of "Scroll to the fixed position element until the position of the element becomes smaller than 0px from the lower end of the viewport by scrolling the viewport, and become the relative arranged element when larger than 0px from the lower end".
Why does specifying the bottom:0
result in the opposite of the behavior shown in MDN?
When top: 0
is specified, the relative position is applied when the element does not reach bottom: 0
of the viewport, and when it reaches, fixed position is applied. When bottom: 0
is specified, the opposite is true. The relative position is applied when the element does not reach the bottom: 0
of the viewport, the fixed position is applied when it is reached
I read CSS3 but its mechanism was difficult to read
top
,bottom
value ) determined? It seems to me that the threshold used for comparison differs betweentop: 0
andbottom: 0
. – Enrollment