Calculate the scroll progress of the page
Get the scroll progress of the page as a CSS variable using a few lines of code
- Powered by Scroll-Driven animations
- Defined at the
:root
level (avaiable to all the elements) - Typed using @property
- You can easily use it within any formula
@property --s {
syntax: '<integer>';
inherits: true;
initial-value: 0;
}
:root {
animation: scroll 1s linear;
animation-timeline: scroll();
}
@keyframes scroll {
to {--s: 100}
}
element:before {
content: counter(s) "%";
counter-reset: s var(--s);
}
See the Pen CSS only scroll progress by Temani Afif (@t_afif) on CodePen.