Home > Software design >  Section fixed header overlapping scrollbar
Section fixed header overlapping scrollbar

Time:11-20

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 >
        <nav>

        </nav>
        <div >
            <header>

            </header>
            <div >
                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.

CodePudding user response:

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

<div >
    <nav>

    </nav>
    <div  id="cntnr">
        <header id="hdr">

        </header>
        <div >
            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>

CodePudding user response:

you can add z-index on the .container so its scrollbar will be shown over the rest of the elements:

(notice: a position which is not static (the default) is also needed for z-index to work)

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

I put a 100 so you can also add z-index on other elements in between. but also 1 will do the effect for now

  • Related