Skip to content

Instantly share code, notes, and snippets.

@nichoth
Last active August 1, 2024 17:12
Show Gist options
  • Save nichoth/646e7708f3e438e8318846f369fa1ca2 to your computer and use it in GitHub Desktop.
Save nichoth/646e7708f3e438e8318846f369fa1ca2 to your computer and use it in GitHub Desktop.
FOUC — flash of unstyled content
// see https://stackoverflow.com/questions/3221561/eliminate-flash-of-unstyled-content
//
// The problem with using a css style to initially hide some page
// elements, and then using javascript to change the style back to
// visible after page load, is that people who don't have javascript
// enabled will never get to see those elements.
//
// so, use javascript to both initially hide as well as redisplay
// those elements after page load
//
/**
* FOUC prevention
*/
document.body.style.opacity = 0
document.body.style.visiblity = 'hidden'
window.addEventListener('load', () => {
document.body.style.visibility = 'visible'
document.body.style.opacity = 1
})
// + some CSS for transition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment