How to hide a semi transparent div behind another when scrolling

I’ve got two divs with different semi transparent background colours but they share the same background image. When I scroll up I’m trying to get the background image to scroll but also for the lower div to disappear behind the first (fixed) div.



<div id="nav">
<div id="content">

body {
    margin: 0;
    background-image: url("");
    background-size: 100% auto;
 #nav {
    top: 0;
    position: fixed;
    height: 60px;
    width: 100%;
    background-color: rgba(153, 50, 204, 0.7);
 #content {
    position: relative;
    top: 60px;
    height: 2000px;
    width: 100%;
    background-color: rgba(176, 224, 230, 0.7);

Is there a way to make both the text AND the background colour fade so it appears they are disappearing behind the top div? Or is there another way to arrange the layers or use z-index values that maintains the background image? Anything I try only gets part of the effect.


6 thoughts on “How to hide a semi transparent div behind another when scrolling”

  1. you can use JS to achieve your goal, by setting event to fire on scroll

    use this JS script into your fiddle and try

    var nav = document.querySelector('#nav');
    window.onscroll = function() {
    if (document.documentElement.scrollTop > 20) { = "0.1";
    } else { = "1";

Leave a Comment