【发布时间】:2020-08-02 03:33:14
【问题描述】:
相对定位元素是相对于最近的定位祖先。我找不到有关文档的原因是,为什么当这个父级将其溢出设置为“可见”以外的值时,其相对定位的子级的定位似乎受到了影响。
我在这里复制了这个,尝试取消注释第 12 行:
html {
/* this is required to reproduce the issue */
overflow-y: auto;
}
body {
background-color: hotpink;
position: relative;
margin: 10px;
/* UNCOMMENT LINE 12 AND #APP WILL DISAPPEAR. WHY? */
/* overflow-y: auto; */
}
#app {
position: absolute;
background-color: black;
left: 0;
top: 0;
width: calc(100vw - 20px);
height: calc(100vh - 20px);
color: white;
}
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app">
Uncomment line #12 and I will disappear. Why?
</div>
<script src="src/index.js"></script>
</body>
</html>
这是完全相同的 sn-p,但重新启用了第 12 行:
html {
/* this is required to reproduce the issue */
overflow-y: auto;
}
body {
background-color: hotpink;
position: relative;
margin: 10px;
/* UNCOMMENT LINE 12 AND #APP WILL DISAPPEAR. WHY? */
overflow-y: auto;
}
#app {
position: absolute;
background-color: black;
left: 0;
top: 0;
width: calc(100vw - 20px);
height: calc(100vh - 20px);
color: white;
}
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app">
Uncomment line #12 and I will disappear. Why?
</div>
<script src="src/index.js"></script>
</body>
</html>
这是一个代码沙箱,您可以使用它: https://codesandbox.io/s/falling-cherry-pce0t?file=/src/styles.css
我在这里创建了一个屏幕录像:https://app.usebubbles.com/dtb9vyNadHq8eMm2YsbfAQ/comments-on-codesandbox-io/
我知道粘性定位元素通过滚动机制粘到最近的祖先,滚动机制被定义为溢出设置为“可见”以外的值的元素。我想知道这是否有某种关系。
为什么在定位的祖先上设置溢出会影响其相对定位的孩子的位置? IE。为什么重新启用第12行时,上面的sn-p中#app会消失?
【问题讨论】: