Home > Blockchain >  Hi, I need the card not to be shown while scrolling. The navigation bar has the z-index=999
Hi, I need the card not to be shown while scrolling. The navigation bar has the z-index=999

Time:12-09

Here is the code for the navbar:

display: flex;
width: 100%;
height: 100px;
justify-content: space-between;
align-items: center;
box-shadow: 2px 2px 15px #616161;
position: sticky;
top: 0;
z-index: 1000;

and card:

background: #fff;
width: 80%;
border-radius: 15px;
margin: 20px 0;
max-height: 190px;
box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;

(https://i.stack.imgur.com/tDbyM.png)

I expect the navbar to be displayed over the card.

CodePudding user response:

@chrslg's suggestion is correct. You have to add background-color to your navbar. Like this:

.your_navbar_class {
    display: flex;
    width: 100%;
    height: 100px;
    justify-content: space-between;
    align-items: center;
    box-shadow: 2px 2px 15px #616161;
    position: sticky;
    top: 0;
    z-index: 1000;
    background-color: white; /* Added */
}

CodePudding user response:

Just to demonstrate my point (both to you and to myself :-)), see this snipped.

Your div.nav is over, as you wanted. But we see the content through. If you click on "toggle background" button, you can see that it is indeed, over.

let hasback=false;
document.getElementById("bt").addEventListener("click", function(){
  let nav=document.querySelector("div.nav");
  if(hasback)
    nav.style.backgroundColor='#00000000';
  else
    nav.style.backgroundColor='#fff';
  hasback=!hasback;
});
div.nav {
display: flex;
width: 100%;
height: 100px;
justify-content: space-between;
align-items: center;
box-shadow: 2px 2px 15px #616161;
position: sticky;
top: 0;
z-index: 1000;
}

div.content{
background: #fff;
width: 80%;
border-radius: 15px;
margin: 20px 0;
max-height: 190px;
box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}
<div >
FOO BAR
</div>


<div >
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

<button id=bt>Toggle Background</button>

  • Related