处于粘性位置,何时为“保证金盒”未用作“位置框”?

发布于 2025-02-06 14:54:18 字数 1053 浏览 1 评论 0原文

w3.org在其定义中提到粘性定位,以下:以下:

位置框是其边缘框,除了在其边缘边缘和包含块的相应边缘之间的距离的任何一侧都小于其相应的边缘,该距离使用代替该边缘。

我试图复制这种情况

.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>

现在,粘性元素的边缘边缘与其包含块的相应边缘之间的距离是0,它比其相应的边距小(40px),因此根据W3.org的定义位置框不应是保证金框,而是粘性元素的大小加上0。但是,如您在示例中看到的那样,位置框仍在尊重边距。

请说明为什么位置框与边距框相同。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文