w3.org mentions in it's definition of Sticky positioning, the following:
The position box is its margin box, except that for any side for which the distance between its margin edge and the corresponding edge of its containing block is less than its corresponding margin, that distance is used in place of that margin.
I have tried to replicate this scenario
.container {
height: 100px;
background: lightgray;
overflow: auto;
}
.content {
background: gray;
border: 1px dotted red;
}
button {
position: sticky;
top: 40px;
margin: 30px 0;
}
.spacer {
height: 300px;
}
<div class="container">
<div class="content">
<button>sticky</button>
</div>
<div class="spacer"></div>
</div>
Now the distance between sticky element's margin edge and the corresponding edge of its containing block is 0
which is less than the its corresponding margin(40px
), so according to w3.org's definition the position box should not be the margin box, instead should be sticky element's size plus 0
. But as you can see in the example, position box is still respecting the margin.
Please explain why is position box still same as margin box.
.content
sticky: withtop: 40px;
andmargin: 30px 0;
, there's 40px of space on top, but if you put something liketop: 20px;
andmargin: 30px 0;
, there's 30px of space on top. So it goes against what the definition says. – Tabulator