Skip to content

Instantly share code, notes, and snippets.

@koirikivi
Last active January 18, 2020 09:49
Show Gist options
  • Save koirikivi/7952081952002e95fc4702358fa296be to your computer and use it in GitHub Desktop.
Save koirikivi/7952081952002e95fc4702358fa296be to your computer and use it in GitHub Desktop.
Find elements that cause horizontal scrolling
/*
Paste this in JS console or create a bookmarklet to find which elements cause a horizontal scroll on the page.
Correctly detects even elements that are hidden by css tricks (e.g. clip property).
Requires jQuery but could probably be optimized to work without it with modest effort.
*/
(function() {
var mainSelector = prompt(
'Selector under which to search',
'body'
);
var windowWidth = prompt('Window width', String(window.innerWidth));
windowWidth = parseInt(windowWidth);
if(isNaN(windowWidth)) {
alert('Enter valid window width');
return;
}
console.log('Finding visible elements under ' + mainSelector + ' where the position of right edge is over ' + windowWidth);
var num = 0;
$(mainSelector).find('*').each(function () {
var $this = $(this);
var outerWidth = $this.outerWidth(true);
var offset = $this.offset();
var right = offset.left + outerWidth;
var visible = $this.is(':visible');
if (visible && right > windowWidth) {
console.log($this[0], right);
num++;
}
});
alert('Found ' + num + ' elements. See javascript console');
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment