简体   繁体   English

位置固定元素始终在div内

[英]position fixed element always inside the div

I need to be able to position fixed element always inside the div. 我需要能够将固定元素始终定位在div中。 Problem is occuring when I resize the window. 调整窗口大小时出现问题。 Then the fixed div is always floating above all other elements. 然后,固定div始终浮在所有其他元素之上。 How to prevent that? 如何预防呢? I need that div to be fixed but positioned inside the div . 我需要固定该div,但将其放置在div内。

Here is an example: 这是一个例子:

<div id="main">
    <div id="one" style="background-color:yellow;"></div>
    <div id="two" style="background-color:black;"></div>
    <div id="three" style="background-color:yellow;">
        <div id="four"></div>
    </div>
</div>

CSS: CSS:

#main
{
    position:relative;
    width:1200px;
    top:0;
    bottom:0;
    left:100px;
}
#one,#two,#three
{
    position:relative;
    width:100px;
    height:1000px;
    float:left;
    top:0;
    bottom:0;
}
#four
{
    position:fixed;
    top:50px;
    background-color:blue;
    width:100px;
    height:200px;
}

EXAMPLE try moving horizontal scroll left and right and you will see what is happening. 例如,尝试左右移动水平滚动,您将看到发生了什么。

Change position to relative. 将位置更改为相对。

Example

#four {
    background-color: blue;
    height: 200px;
    position: relative;
    top: 50px;
    width: 100px;
}

Check out this answer on a similar question . 查看关于类似问题的答案 The problem you're facing is something that can't be solved with CSS alone, unfortunately. 不幸的是,您面临的问题是仅靠CSS无法解决的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM