Search code examples
htmlcssheaderscrollbaroverlap

Section fixed header overlapping scrollbar


i'm pretty new to html and css. Browsed through previously asked similar questions but non of the solutions seems to work for me. Basically I have this situation:

Situation.

The desired effect is the content to be visible through the semi-transparent header, but the header shouldn't overlap the scrollbar.

HTML is

<body>
    <div class="flex">
        <nav>

        </nav>
        <div class="container">
            <header>

            </header>
            <div class="content">
                some random text
            </div>
        </div>
    </div>
    <footer>

    </footer>
</body>

CSS is

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

.flex{
    display: flex;
}

nav{
    flex: 0 0 20rem;
    background-color: black;
    height: 90vh;
}

.container{
    background-color: blue;
    flex-grow: 1;
    height: 90vh;
    overflow-y: auto;
    padding-top: 100px;
}

header{
    height: 80px;
    position: fixed;
    top: 0;
    left: 20rem;
    right: 0;
    z-index: 1;
    background-color: rgba(0, 0, 0, 0.5);

}
.content{
    height: 2000px;
    color: white;
}

footer{
    height: 10vh;
    background-color: gray;
}


Only solution I've found is to put a value into header {right} equal to the width of the scrollbar, but that's of course not reliable for all browsers, so it's just a trick, not a real solution.

Tried using sticky but that way header doesn't overlap content as desired.

Tried to put header directly inside content but it doesn't work neither.


Solution

  • If I am not wrong, there is no CSS only solution. You have to add JS. Here is the code.

    <div class="flex">
        <nav>
    
        </nav>
        <div class="container" id="cntnr">
            <header id="hdr">
    
            </header>
            <div class="content">
                some random text
            </div>
        </div>
    </div>
    <footer>
    
    </footer>
    

    CSS

    *{
        padding: 0;
        margin: 0;
        box-sizing: border-box;
    }
    
    .flex{
        display: flex;
    }
    
    nav{
        flex: 0 0 20rem;
        background-color: black;
        height: 90vh;
    }
    
    .container{
        background-color: blue;
        flex-grow: 1;
        height: 90vh;
        overflow-y: auto;
        padding-top: 100px;
        position: relative;
    }
    .container.sticky{
        padding-top:0px;
    }
    
    header{
        height: 80px;
        top: 0;
        left:0;
        width:100%;
        z-index: 1;
        background-color: rgba(0, 0, 0, 0.5);
        position:absolute;
    
    
    }
    header.sticky{position:sticky;top:0}
    .content{
        height: 2000px;
        color: white;
    }
    
    footer{
        height: 10vh;
        background-color: gray;
    }
    

    JS

    <script type="text/javascript">
        var cntnrScrll = document.getElementById("cntnr");
        var hdr = document.getElementById("hdr");
    
    cntnrScrll.onscroll = function()  { scrollFunction() };
    
    function scrollFunction() {
      if (cntnrScrll.scrollTop > 0 ) {
        hdr.classList.add("sticky");
        cntnrScrll.classList.add("sticky");
      } else {
        hdr.classList.remove("sticky");
        cntnrScrll.classList.remove("sticky");
      }
    }
    </script>