Fixed position but relative to container

后端 未结 25 1594
独厮守ぢ
独厮守ぢ 2020-11-21 23:52

I am trying to fix a div so it always sticks to the top of the screen, using:

position: fixed;
top: 0px;
right: 0px;

However,

相关标签:
25条回答
  • 2020-11-22 00:28

    I had to do this with an advertisement that my client wanted to sit outside of the content area. I simply did the following and it worked like a charm!

    <div id="content" style="position:relative; width:750px; margin:0 auto;">
      <div id="leftOutsideAd" style="position:absolute; top:0; left:-150px;">
        <a href="#" style="position:fixed;"><img src="###" /></a>
      </div>
    </div>
    
    0 讨论(0)
  • 2020-11-22 00:29

    This is easy (as per HTML below)

    The trick is to NOT use top or left on the element (div) with "position: fixed;". If these are not specified, the "fixed content" element will appear RELATIVE to the enclosing element (the div with "position:relative;") INSTEAD OF relative to the browser window!!!

    <div id="divTermsOfUse" style="width:870px; z-index: 20; overflow:auto;">
        <div id="divCloser" style="position:relative; left: 852px;">
            <div style="position:fixed; z-index:22;">
                <a href="javascript:hideDiv('divTermsOfUse');">
                    <span style="font-size:18pt; font-weight:bold;">X</span>
                </a>
            </div>
        </div>
        <div>  <!-- container for... -->
             lots of Text To Be Scrolled vertically...
             bhah! blah! blah!
        </div>
    </div>
    

    Above allowed me to locate a closing "X" button at the top of a lot of text in a div with vertical scrolling. The "X" sits in place (does not move with scrolled text and yet it does move left or right with the enclosing div container when the user resizes the width of the browser window! Thus it is "fixed" vertically, but positioned relative to the enclosing element horizontally!

    Before I got this working the "X" scrolled up and out of sight when I scrolled the text content down.

    Apologies for not providing my javascript hideDiv() function, but it would needlessly make this post longer. I opted to keep it as short as possible.

    0 讨论(0)
  • 2020-11-22 00:32

    2019 SOLUTION: You can use position: sticky property.

    Here is an example CODEPEN demonstrating the usage and also how it differs from position: fixed.

    How it behaves is explained below:

    1. An element with sticky position is positioned based on the user's scroll position. It basically acts like position: relative until an element is scrolled beyond a specific offset, in which case it turns into position: fixed. When it is scrolled back it gets back to its previous (relative) position.

    2. It effects the flow of other elements in the page ie occupies a specific space on the page(just like position: relative).

    3. If it is defined inside some container, it is positioned with respect to that container. If the container has some overflow(scroll), depending on the scroll offset it turns into position:fixed.

    So if you want to achieve the fixed functionality but inside a container, use sticky.

    0 讨论(0)
  • 2020-11-22 00:32

    With pure CSS you can't manage to do it; at least I haven't. However you can do it with jQuery very simply. I'll explain my problem, and with a little change you can use it.

    So for a start, I wanted my element to have a fixed top (from top of the window), and a left component to inherit from the parent element (because the parent element is centered). To set the left component, just put your element into the parent and set position:relative for the parent element.

    Then you need to know how much from the top your element is when the a scroll bar is on top (y zero scrolled); there are two options again. First is that is static (some number) or you have to read it from the parent element.

    In my case it's 150 pixels from top static. So when you see 150 it's how much is the element from the top when we haven't scrolled.

    CSS

    #parent-element{position:relative;}
    #promo{position:absolute;}
    

    jQuery

    $(document).ready(function() { //This check window scroll bar location on start
        wtop=parseInt($(window).scrollTop());
        $("#promo").css('top',150+wtop+'px');
    
    });
    $(window).scroll(function () { //This is when the window is scrolling
        wtop=parseInt($(window).scrollTop());
        $("#promo").css('top',150+wtop+'px');
    });
    
    0 讨论(0)
  • 2020-11-22 00:33

    Just take the top and left styles out of the fixed position div. Here's an example

    <div id='body' style='height:200%; position: absolute; width: 100%; '>
        <div id='parent' style='display: block; margin: 0px auto; width: 200px;'>
            <div id='content' style='position: fixed;'>content</div>
        </div>
    </div> 
    

    The #content div will be sit wherever the parent div sits, but will be fixed there.

    0 讨论(0)
  • 2020-11-22 00:33

    Disclaimer:

    This answers just the title: Fixed position but relative to container. For the actual use case the user is asking for position: sticky is the way to go.


    https://developer.mozilla.org/en-US/docs/Web/CSS/position

    It is positioned relative to the initial containing block established by the viewport, except when one of its ancestors has a transform, perspective, or filter property set to something other than none

    You just need to add a transform to the container and the position of the fixed element will be relative to the container. I think a transform: translateX(0); should be enough.

    0 讨论(0)
提交回复
热议问题