In case you need better compatibility, here a bit adjusted Wendelin's answer.
function visible(elem) {
return !(elem.clientHeight === 0 || elem.clientWidth === 0)
}
let first;
let firstOffY;
var elems = [], source = document.querySelectorAll('body > *');
for(var elemI=0;elemI<source.length;elemI++) {
elems.push(source[elemI]);
}
let allVisible = elems.filter(visible)
for(var elem in allVisible) {
elem = allVisible[elem];
//Calculaate the offset to the document
//See: https://stackoverflow.com/a/18673641/7448536
const offY = elem.getBoundingClientRect().top + document.documentElement.scrollTop
if (first == null || offY < firstOffY) {
first = elem;
firstOffY = offY;
}
}
console.log(first.outerHTML);
first.classList.add('highlight');
.highlight {
background-color: yellow;
}
<div>
<p>Some Text</p>
<h1>Headline</h1>
</div>