position: sticky Has Been in WordPress Core Since 6.2 and Almost Nobody Knows
Did you know WordPress's Gutenberg editor has a position: sticky setting? Don't worry, I didn't either until very recently. I suppose I can't be forgiven given that I've been a WordPress developer for well over a decade. Before we get into how the position: sticky works in the editor, here's a refresher on what it is and what it does.
The Position Property
The position property determines how an element is positioned in a document. An element can be positioned based on its relationship to the <body> or its parent. How we go about positioning elements is setting the following predefined values on the position property:
- static: the default value, within document flow
- relative: The element is positioned according to the normal flow of the document, and then offset relative to itself
- absolute: The element is removed from the normal document flow, and is positioned relative to its closest positioned ancestor
- fixed: The element is removed from the normal document flow, and is positioned relative to the viewport in most cases
- sticky: The element is positioned according to the normal flow of the document, and then offset relative to its nearest scrolling ancestor and containing block
All of the properties above with the exception of static can be controlled with the corresponding offset properties top, right, bottom, left. Logical properties can also be used to adjust position: inset-block-start, inset-block-end, inset-inline-start, inset-inline-end.
Position: Sticky: Use Cases
So why would you want to use position: sticky in the first place? Have you ever seen one of those cool side navigation menus that pin themselves to the top of the page as you scroll? That's why. Menus and navigation aren't the only reason why you might want to consider using position: sticky. We will highlight more advanced and useful use cases later in this article.
Position: Sticky: In Gutenberg
Now that you have an idea of how position: sticky is supposed to work, let's see its application inside the Gutenberg editor:
Pretty cool, wouldn't you say? It can definitely improve user experience. But how is this different from position: fixed? When an element has the position: sticky setting, it remains in the layout flow. The element scrolls normally until it reaches the threshold set by top (or inset-block-start), then "sticks" to its parent container until its container ends. A fixed element leaves the flow entirely.
Position: Sticky: More Advanced Use Cases with Glide
While setting the position: sticky in Gutenberg, you'll quickly notice that it's barebones and missing positioning properties. Let's explore some more advanced use cases with the Glide plugin and how it can potentially improve user experience and retention using its more advanced position: sticky features.
Table of Contents
If you have a website that's heavy on documentation, a modified sticky bar containing the table of contents for the page you are viewing comes in handy. Readers are able to track their progress and navigate long-form content without jumping up and down the page.
Section and List Headers
We can also pin category titles to the viewport, while scrolling through the page content and items, while the next header pushes the previous header out of the way.
Scrollytelling
"Scrollytelling" is a mix of scrolling and storytelling, made possible by position: sticky. It's a digital narrative format in which scrolling up and down a webpage triggers an action, animation, graphic(s), video, audio and/or any interactive element in sequence. Scrolling allows the reader to engage with the content at their own pace.
Position: Sticky: The Takeaway
position: sticky has been hiding in plain sight in the block editor since WordPress 6.2. On a block theme, select a Group, open its Position setting, choose Sticky, and it holds at the top of the window while the page scrolls. For a site header or a short sidebar, that's often all you need.
When you want sticky to do more than hold one block in place, Glide picks up where core stops. The free edition gives long pages a table of contents that follows the reader. Glide Pro adds section headers that hand off to one another, and scrollytelling layouts, among them a media pane that changes its picture as the words move past.
Try the values in the CodePen above, try Sticky on a Group block of your own, and when you want more, Glide is free on WordPress.org.