Fixed positioned div within a relative parent div

后端 未结 9 1876
春和景丽
春和景丽 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:36

    A simple thing you can do is position your fixed DIV relative to the rest of your page with % values.

    Check out this jsfiddle here where the fixed DIV is a sidebar.

    div#wrapper {
        margin: auto;
        width: 80%;
    }
    
    div#main {
        width: 60%;
    }
    
    div#sidebar {
        position: fixed;
        width: 30%;
        left: 60%;
    }
    

    And a brief picture below describing the layout above:

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

    here is a more generic solution, that don't depends on the Menu/Header height. its fully responsive, Pure CSS solution, Works great on IE8+, Firefox, Chrome, Safari, opera. supports Content scrolling without affecting the Menu/Header.

    Test it with that Working Fiddle

    The Html:

    <div class="Container">
        <div class="First">
            <p>The First div height is not fixed</p>
            <p>This Layout has been tested on: IE10, IE9, IE8, FireFox, Chrome, Safari, using Pure CSS 2.1 only</p>
        </div>
        <div class="Second">
            <div class="Wrapper">
                <div class="Centered">
                    <p>The Second div should always span the available Container space.</p>
                    <p>This content is vertically Centered.</p>
                </div>
            </div>
        </div>
    </div>
    

    The CSS:

    *
    {
        margin: 0;
        padding: 0;
    }
    
    html, body, .Container
    {
        height: 100%;
    }
    
        .Container:before
        {
            content: '';
            height: 100%;
            float: left;
        }
    
    .First
    {
        /*for demonstration only*/
        background-color: #bf5b5b;
    }
    
    .Second
    {
        position: relative;
        z-index: 1;
        /*for demonstration only*/
        background-color: #6ea364;
    }
    
        .Second:after
        {
            content: '';
            clear: both;
            display: block;
        }
    
    /*This part his relevant only for Vertically centering*/
    .Wrapper
    {
        position: absolute;
        width: 100%;
        height: 100%;
        overflow: auto;
    }
        .Wrapper:before
        {
            content: '';
            display: inline-block;
            vertical-align: middle;
            height: 100%;
        }
    
    .Centered
    {
        display: inline-block;
        vertical-align: middle;
    }
    
    0 讨论(0)
  • 2020-12-05 18:40

    you can fix the wrapper using absolute positioning. and the give inside div a fixed position.

    .wrapper{
     position:absolute;
     left:10%;// or some valve in px
     top:10%; // or some valve in px
     }
    

    and div inside that

    .wrapper .fixed-element{ 
    position:fixed;
    width:100%;
    height:100%;
    margin-left:auto; // to center this div inside at center give auto margin
    margin-right:auto;
    }
    

    try this It might work for you

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