JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="src/style.css">
</head>
<body>
<div class="container">
<div class="first"></div>
<div class="second">
<div class="test">Sticky Element</div>
</div>
<div class="third"></div>
</div>
</body>
</html>
CSS
.first {
height: 100vh;
background-color: red;
position: relative;
z-index: 2; /* Increase z-index to ensure it covers .test */
}
.second {
height: 100vh;
background-color: yellow;
position: relative;
z-index: -1;
}
.test {
width: 95%;
height: 50px;
background-color: green;
margin: 0 auto;
position: sticky;
top: 70%;
z-index: -1;
}
.third {
height: 100vh;
background-color: blue;
position: relative;
}