Fixed positioned div within a relative parent div

后端 未结 9 1875
春和景丽
春和景丽 2020-12-05 17:40

I am currently building a responsive website and need a menu to be fixed, thus not scrolling when the rest of the site scrolls. the issue is that it is a fluid layout and i

相关标签:
9条回答
  • 2020-12-05 18:20

    This question came first on Google although an old one so I'm posting the working answer I found, which can be of use to someone else.

    This requires 3 divs including the fixed div.

    HTML

    <div class="wrapper">
        <div class="parent">
            <div class="child"></div>
        </div>
    </div>
    

    CSS

    .wrapper { position:relative; width:1280px; }
        .parent { position:absolute; }
            .child { position:fixed; width:960px; }
    
    0 讨论(0)
  • This is possible if you move the fixed <div> using margins and not positions:

    #wrap{ position:absolute;left:100px;top:100px; }
    #fixed{ 
       position:fixed;
       width:10px;
       height:10px;
       background-color:#333;
       margin-left:200px;
       margin-top:200px;
    }
    

    And this HTML:

    <div id="wrap">
       <div id="fixed"></div>
    </div>
    

    Play around with this jsfiddle.

    0 讨论(0)
  • 2020-12-05 18:21

    Gavin,

    The issue you are having is a misunderstanding of positioning. If you want it to be "fixed" relative to the parent, then you really want your #fixed to be position:absolute which will update its position relative to the parent.

    This question fully describes positioning types and how to use them effectively.

    In summary, your CSS should be

    #wrap{ 
        position:relative;
    }
    #fixed{ 
        position:absolute;
        top:30px;
        left:40px;
    }
    
    0 讨论(0)
  • 2020-12-05 18:27

    Try postion:sticky on parent element.

    0 讨论(0)
  • 2020-12-05 18:31

    Sample solution. Check, if this is what you need.

    <div class="container">
       <div class="relative">      
          <div class="absolute"></div>      
          <div class="content">
            <p>
              Content here
            </p>
          </div>
        </div>
     </div>
    

    And for CSS

    .relative { 
      position: relative;
    }
    
    .absolute {
      position: absolute;
      top: 15px;
      left: 25px;   
    }
    

    See it on codepen https://codepen.io/FelySpring/pen/jXENXY

    0 讨论(0)
  • 2020-12-05 18:32

    An easy solution that doesn't involve resorting to JavaScript and will not break CSS transforms is to simply have a non-scrolling element, the same size as your scrolling element, absolute-positioned over it.

    The basic HTML structure would be

    CSS

    <style>
        .parent-to-position-by {
            position: relative;
            top: 40px; /* just to show it's relative positioned */
        }
        .scrolling-contents {
            display: inline-block;
            width: 100%;
            height: 200px;
            line-height: 20px;
            white-space: nowrap;
            background-color: #CCC;
            overflow: scroll;
        }
        .fixed-elements {
            display: inline-block;
            position: absolute;
            top: 0;
            left: 0;
        }
        .fixed {
            position: absolute; /* effectively fixed */
            top: 20px;
            left: 20px;
            background-color: #F00;
            width: 200px;
            height: 20px;
        }
    </style>
    

    HTML

    <div class="parent-to-position-by">
        <div class="fixed-elements">
            <div class="fixed">
                I am &quot;fixed positioned&quot;
            </div>
        </div>
        <div class="scrolling-contents">
            Lots of contents which may be scrolled.
        </div>
    </div>
    
    • parent-to-position-by would be the relative div to position something fixed with respect to.
    • scrolling-contents would span the size of this div and contain its main contents
    • fixed-elements is just an absolute-positioned div spanning the same space over top of the scrolling-contents div.
    • by absolute-positioning the div with the fixed class, it achieves the same effect as if it were fixed-positioned with respect to the parent div. (or the scrolling contents, as they span that full space)

    Here's a js-fiddle with a working example

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