my goal is to set the sticky div initially to visibility: hidden. After scrolling more than 200px, the button should appear with visibility: visible. Is there a workaround to achieve this only with CSS, without JavaScript? In pure CSS, there doesn’t seem to be a way to detect exact scroll distances like 200px. Any suggestions?
This is my example:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
margin: 0;
padding: 0;
}
header {
height: 150px;
background-color: black;
}
.container {
height: 1500px;
background-color: azure;
}
footer {
height: 500px;
background-color: grey;
}
.sticky {
display: flex;
justify-content: flex-end;
position: sticky;
bottom: 10px;
margin:10px;
}
.sticky a {
background-color: black;
color: #fff;
padding: 5px;
}
</style>
</head>
<body>
<header></header>
<div class="container"></div>
<div class="sticky">
<a href="#top">ScrollToTop</a>
</div>
<footer></footer>
</body>
</html>
I tried many things like a sticky spacer, semi-sticky with multiple elements, and so on. Unfortunately, nothing worked.
2
Answers
CSS Scroll animation
It can be implemented similarly, but adjust it yourself.
Using JS
It becomes simple and accurate.
You can also use a grid-layout, container query (to toggle an hide/show) and float/clear-both to show & push down a sticky link if needed only:
example from my earlier comment with your HTML: