
I’m not doing much to the sticky elements, except styling them. Position: -webkit-sticky /* Required for Safari */ footer elements will get position:sticky along with a placement property saying where on the screen they’ll start “sticking” when scrolled.category, Here’s the HTML we’re working with… basically two articles:
The third element is a footer that slides out of the article and is revealed when the article is scrolled above a certain threshold. The second is the title of the article and it stays visible at the top of the screen, while the body of the content disappears behind it on scroll (which is the typical sticky element behavior). The first one is the category header that slides under the body of the article once it reaches the top of the screen. There are three sticky elements in our example: See the Pen Slide In and Out Effect using “position:sticky” by Preethi Sam ( on CodePen. Well, guess what? We can do that with sticky elements! For example, a header that slides out and a footer that slides in: Say we want to create an effect where elements either slide in or out of view on scroll - sort of like parallax. See the Pen position:sticky (CSS) by CSS-Tricks ( on CodePen. Compare sticky example above with this one that uses the same concept using a fixed element instead: The difference? A sticky element remains confined to the parent container it is in. Sticky elements ( position: sticky ) are very similar to fixed elements ( position: fixed ) in that they both maintain their position on the screen, even as the user scrolls up or down the page. See the Pen position:sticky (CSS) by Preethi Sam ( on CodePen.
As cool as that is, we can also hide elements in the same way!
position: sticky, part of Philip Walton’s Polyfill.Sticky elements are predominantly used for keeping something shown on the screen throughout scrolling. position–sticky- by Matthew Phillips (Uses Modernizr for detection). fixed-sticky by the Filament Group (requires jQuery). See position: sticky on Can I Use… for all the details.įortunately there a number of polyfills to choose from: Internet Explorer – No support ( see status). Opera 23+ – Supported by enabling “experimental Web Platform features” in about:flags. Safari 6.1+ – Supported using the -webkit vendor prefix on the value (i.e. You can read the bug report explaining the removal, but we suspect the feature will be re-implemented shortly, and possibly without a disruption in the stable version’s support. Chrome 38(?) – The Chrome team have recently removed this feature from Blink, so it’s currently not available in Chrome Canary (version 38.x), even with the flag. Chrome 23+ – Supported by enabling “experimental Web Platform features” in chrome://flags. Firefox 26+ – Supported by setting to “true” under about:config. Here’s how things stack up for each browser: The support for this new value is pretty poor at the moment. Now that you have a good grasp of what the problem is and what’s a possible JavaScript-based solution, it’s time to embrace modernity and discuss what this position: sticky is all about. See the Pen position: sticky with JS, improved by SitePoint ( on CodePen. The final result is shown in this next demo: addEventListener ( 'scroll', function ( ) querySelector ( '.menu' ) var menuPosition = menu. Specifically, we need to add top: 0 and position: fixed to the menu when it’s at the top of the viewport, and then revert the properties back to their defaults otherwise.Īn alternative, but similar, approach is to create a class in our CSS where those values are present, and then add and remove the class as needed, with JavaScript, which might look like this: var menu = document.
We listen for the scroll event of the page and use JavaScript to change the value of the position and top properties according to the current position of the viewport. To achieve this with traditional code we need to add some JavaScript. What we want to achieve is that when the user scrolls the page, as soon as the menu is positioned at the top of the viewport, instead of the menu scrolling out of view, it will stick at the top position - as if it had a position: fixed applied to it (only when it reaches the top of the viewport). See the Pen Example with no sticky menu by SitePoint ( on CodePen. Let’s pretend the main menu of our amazing website is right after the header but still at the top of the page (not in a sidebar) and that it occupies all the width available. Before discussing this new value for the position property, let’s better understand what is the problem we’re trying to solve.