October 26, 2024
Chicago 12, Melborne City, USA
HTML

How to show a sticky button after scrolling 200px without JavaScript?


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.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video