求大佬帮忙解析一段css sticky定位代码?

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title></title>
</head>

<body>

    <style>
        @import url(https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css);

        .top-navbar {
            height: 70px;
            background-color: rgb(250, 200, 200);
        }

        .app-container {
            height: calc(100vh - 70px);
            background-color: rgb(200, 250, 200);

        }

        .info {
            height: 175px;
            background-color: rgb(200, 200, 250);
        }

        .router {
            width: 100%;
            min-height: 150vh;
            background-color: rgb(120, 50, 250);
        }

        /* ----- 核心代码 ----- */

        .app-container {
            overflow: auto;
        }

        .main {
            display: flex;
        }

        .sticky-box {
            position: sticky;
            top: 0;
        }
    </style>

    <div class="box-wrapper">
        <div class="top-navbar">
            <h1>Part1</h1>
        </div>
        <div class="app-container">
            <div class="info">
                <h1>Part2</h1>
            </div>
            <main class="main">
                <div class="side-navbar">
                    <div class="sticky-box">
                        <h1>Part3</h1>
                    </div>
                </div>
                <div class="router">
                    <h1>Part4</h1>
                </div>
            </main>
        </div>
    </div>
</body>

</html>

image.png

请注意图中, 滚动条是app-container标签里的
按照道理来说, 在app-container之内, sticky定位只能生效Part2和main标签(因为只能生效直系子元素, 我是这么认为的)
但是为什么, 以上css这样写, 我可以让Part3实现sticky定位?
有没有大佬知道是什么原因??

阅读 1.4k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题