These days I had to rewrite a few elements on different pages to use a position sticky. Mainly navigations menus needed to be sticky at the top of the screen as the user scrolls down.
While refactoring these components I've seen a few common issues that were preventing the position:sticky to do its job. Let's see them one by one:
- Position sticky works only if any of the
top,left,bottom,rightprops is set. It will not work without one of these values. - Check if a parent element has the overflow property set to
scroll,hidden, orauto. If the overflow of a parent is set to anything else than the default value (visible) then it may break your sticky element. - And maybe the most tricky: if the direct ancestor of the sticky element has
display:flexthen be sure to have on the sticky elementalign-self: flex-start.
By the way, if you are interested to find out more about CSS layout and other frontend tips you can sign up for my newsletter. You can check out here and here some examples of the js-craft newsletter.
Found this article to be quite helpful while debugging the "stickiness" of an element. Cool stuff! I really like the JS script from the article. You can paste in the dev tools to check if the overflow of a parent breaks the sticky position:
let parent = document.querySelector('.sticky').parentElement;
while (parent) {
const hasOverflow = getComputedStyle(parent).overflow;
if (hasOverflow !== 'visible') {
console.log(hasOverflow, parent);
}
parent = parent.parentElement;
}
๐ Build a full trivia game app with LangChain
Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js
๐ Build a full trivia game app with LangChain
Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js