I want to create a unique design with overlapping divs that can be selectively displayed through scrolling. While this effect is possible with images using background-attachment: fixed
, I am seeking a solution that can work for any child element.
Here is an example of the desired effect with images:
.main {
position: relative;
height: 100vh;
overflow-y: scroll;
}
.scroll-block {
height: 100vh;
width: 100vw;
overflow: hidden;
background-size: cover;
background-attachment: fixed;
background-position: center;
}
<html class="overflow-hidden">
<head>
<link rel="stylesheet" href="/public/tailwind.css">
</head>
<body class="main">
<div class="scroll-block" style="background-image: url(http://placekitten.com/500/500)">
</div>
<div class="scroll-block" style="background-image: url(http://placekitten.com/400/400)">
</div>
<div class="scroll-block" style="background-image: url(http://placekitten.com/300/300)">
</div>
</body>
</html>
I attempted to use fixed/absolute divs inside the scrollable blocks while setting overflow: hidden
, but the overflow property did not affect fixed/absolute divs. Is there a CSS way to achieve this?