Skip navigation
CSS Tip! 🍬 You can create a CSS-only sticky CTA using position: sticky or scroll-driven animations 🤙…
0:00 / 0:29

CSS Tip! 🍬 You can create a CSS-only sticky CTA using position: sticky or scroll-driven animations 🤙…

jhey ʕ•ᴥ•ʔ

@jh3yy146K followers

Visit on X
826 likesDownloadOriginal post

133,020 views

CSS Tip! 🍬 You can create a CSS-only sticky CTA using position: sticky or scroll-driven animations 🤙 .cta { position: sticky; margin-top: 110vh; bottom: 2rem; /* 👈 Stick! */ } This is one way 👀 This first way relies on you setting a layout on the body and putting the CTA in a zero-space part of the layout body { display: grid; grid-template-columns: auto 0; } The children of the body are an element with your content and then the CTA. You could also use display:flex too. .content { flex: 1 0 100%; } .cta { place-self: end; } As you scroll the body, the CTA comes into view and sticks in position 🙌 That's one way. If you want to take it further and do something like flip between showing or not, maybe scale it up, or add some special easing, etc. an animation is another way 📜 First, change the styles for your CTA. Note the translate property that's powered by a custom property .cta { position: fixed; bottom: 2rem; right: 2rem; translate: 0 calc(20vh - (var(--show) * 20vh)); transition: translate 0.875s var(--elastic); } Next you need a custom property that you're going to animate @​property --show { inherits: true; initial-value: 0; syntax: ' '; } Lastly, you animate this value on the body. As the property value changes, the value will trickle down to the CTA @​supports (animation-timeline: scroll()) { body { animation: show-cta both steps(1); animation-timeline: scroll(root); animation-range: 0 10vh; } @​keyframes show-cta { to { --show: 1; } } } Using @​supports you can use this as a progressive enhancement. If scroll-driven animations are supported, use them. Otherwise fallback to using position: sticky 🤙 That's it! As always, any questions or requests, hit me up! 🙏 CodePen.IO link below! 👇

10 Comments

Comments from the original post on X (Twitter)

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Here's that @CodePen link! 🤙 Tried it out in Chrome, Safari, Firefox, and iOS. Should be all good 🙏

    53 likes

  • Jason Torres's profile picture

    @TasonJorres

    Just plain wow man.

    5 likes

  • freddie's profile picture

    @frederic_ooo

    the margin top idea is already genius by itself, but after reading the scroll driven animation code, I’m so happy to be living in this day and age of css

    2 likes

  • Okorite's profile picture

    @ah3mba

    wow

    1 like

  • リ 乇 刀 卂 丅 丂 口 乙 乇 𠘨's profile picture

    @vedatsozen

    I make it by using scrollY .. I make it visible when scrollY is more than a specific pixel.

    1 like

  • jiafeng wang's profile picture

    @jiafengwang_yo

    nice

    1 like

  • Abhishek Rawat's profile picture

    @iamabhishek22_

    Just the time I decide that I've known enough CSS, this man tweets. 😅😭

    1 like

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Haha 🙏

  • George's profile picture

    @__jorgeserrano

    You are god.

  • Joshsanger.eth's profile picture

    @JoshSanger_eth

    This is awesome. Love the different approaches. The animation-timeline is my fav! Can't wait for Safari to get there. Also the number of open tabs is the cherry on top! 🙈😅