Skip to content

Instantly share code, notes, and snippets.

@gkielwasser
Forked from brandonjp/findOverflowParents.js
Created March 20, 2021 20:07
Show Gist options
  • Save gkielwasser/d50ceb8814d8ab20855bb27433666c83 to your computer and use it in GitHub Desktop.
Save gkielwasser/d50ceb8814d8ab20855bb27433666c83 to your computer and use it in GitHub Desktop.
find overflow:hidden ancestors
// when you're trying to use `position:sticky` on an element
// you'll have trouble if any parent element has an overflow of "hidden"
// so, to find those troublesome parents...
// copy & paste this into Chrome Inspector/Dev Tools console
// (and be sure to change the #stickyElement below, if needed)
function findOverflowParents(element, initEl) {
function hasOverflow(el) {
let overflow = getComputedStyle(el).overflow;
return overflow.indexOf("hidden") > -1;
}
let thisEl = element;
let origEl = initEl || thisEl;
if (hasOverflow(thisEl)) console.warn("Overflow found on:", thisEl.tagName, '#' + thisEl.id, '.' + thisEl.className, thisEl);
if (thisEl.parentElement) {
return findOverflowParents(thisEl.parentElement, origEl);
} else {
return console.info('Overflow ancestor check complete! original element:', origEl);
}
}
// to use a selector, change `#stickyElement` to your desired selector
// findOverflowParents(document.querySelector('#stickyElement'));
// or use $0 for the last element selected in the Chrome Inspector
findOverflowParents($0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment