如何设置粘性元素的高度?

发布于 2025-02-06 13:50:15 字数 1306 浏览 1 评论 0原文

这是粘性元素的一个简单示例,但我想知道为什么我不能将高度设置为粘性元素(绿色块)。

.container {
    background: red;
    display: flex;
    flex-direction: column;
    height: 30vh;
    width: 150px;
    overflow: auto;
   /* position: fixed; */
}

.btn {
    background: green;
    position: sticky;
     height: 50px; /* why does not work?!! */
    bottom: 0;
    left: 0;
    right: 0;
}
<div class='container'>
        <div class='items'>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
        </div>

        <button class='btn'>+</button>
    </div>

我删除了位置:固定,但仍然无法正常工作!

This is a simple example of sticky element, but I was wondering why I cant set a height to the sticky element (the green block).

.container {
    background: red;
    display: flex;
    flex-direction: column;
    height: 30vh;
    width: 150px;
    overflow: auto;
   /* position: fixed; */
}

.btn {
    background: green;
    position: sticky;
     height: 50px; /* why does not work?!! */
    bottom: 0;
    left: 0;
    right: 0;
}
<div class='container'>
        <div class='items'>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
            <div>hello world </div>
        </div>

        <button class='btn'>+</button>
    </div>

I removed the position: fixed but still not working!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2025-02-13 13:50:15

看图片,这是我在检查摘要时拍摄的快照,因为您可以看到绿色吧台具有您分配给它的完整高度,但是有些没有显示它。完整的高度。

这是另一个快照,我在整页上运行片段时拍摄了它。在这里,绿色条使用其分配的高度(50px)清楚地看到。

尽管您的标记样式不正确,但您将粘性元素保留在另一个固定元素中。您想实现什么目标?

Snapshot of your snipped

Look at the picture, this is a snapshot that I took while inspecting your snippet, as you can see the green bar has its complete height that you assigned to it, but some how it is not showing the complete height.

Another snapshot

This is another snapshot, that I took while running your snippet on full page. Here it is clearly visible that the green bar is using its assigned height (50px).

Although the way that you're styling your markup is not correct, you're keeping a sticky element within another fixed element. What exactly is it that you want to achieve?

爱要勇敢去追 2025-02-13 13:50:15

对于仍然存在问题的任何人,它似乎确实是与Flex容器和引起问题的粘性子元素的互动。您可以尝试添加填充物,而不是指定高度。那对我有用。 (我会在@ddmteetu的主题下发布此信息,但我还没有足够的声誉)

For anyone still having an issue with this, it does seem like its the interaction with a flex container and the sticky child element causing the issue. Instead of specifying the height, you can try adding padding instead. That worked for me. ( i would have posted this under @Ddmteetu 's thread but i dont have enough reputation yet)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文