I have :
<body>
<div class="wrapper">
<div class="sidebar">
<div class="button"></div>
sidebar
</div>
<div class="content">
<div class="header">header</div>
<div class="body">body</div>
</div>
</div>
</body>
and the css:
.wrapper {
display: flex;
.sidebar {
width: 200px;
position: sticky;
top: 0;
background-color: red;
height: 100vh;
.button {
position: absolute;
top: 0;
right: 0;
translate: 10px 10px;
width: 30px;
height: 30px;
background-color: black;
z-index: 10;
}
}
.content {
flex-grow: 1;
.header {
position: sticky;
top: 0;
height: 56px;
background-color: yellow;
}
.body {
height: 3000px;
}
}
}
I want the .button
is over the header, but:
the .button
is behind .header
. I tried to chang z-index
but it not working. Both .sidebar
and .header
is position: sticky;
.
I also tried something to find the problem out but nothing.
If I create an absolute
element in .header
, everything good.
How I can fix this
2
Answers
Try this it will solve your issue…Just add z-index -1 in .header
class. I have mentioned in below code….hope it will work…let me know if not…Thank you…
I made some changes to your code to ensure that the button is positioned inside the header div but still above it:
HTML
Here I created a new div "nav-buttons" presuming that the header will have more buttons (if not just remove the other buttons).
CSS
Here I added a
display: flex
and aflex-direction: column
to your header because I don’t know if you want to keep the "header" placeholder, otherwise you can remove it. Also, I added amargin: 0.5rem
to the nav-buttons div to give a space between them and the header, you can adjust it according to your needs.