Skip to content

Instantly share code, notes, and snippets.

@johnpolacek
Last active April 15, 2026 14:24
Show Gist options
  • Select an option

  • Save johnpolacek/3827270 to your computer and use it in GitHub Desktop.

Select an option

Save johnpolacek/3827270 to your computer and use it in GitHub Desktop.
Prevent FOUC
<!-- Prevent FOUC (flash of unstyled content) - http://johnpolacek.com/2012/10/03/help-prevent-fouc/ -->
<style type="text/css">
.no-fouc {display: none;}
</style>
<script type="text/javascript">
document.documentElement.className = 'no-fouc';
// add to document ready: $('.no-fouc').removeClass('no-fouc');
</script>
@xuzeshen
Copy link
Copy Markdown

great!

@jason-rogers-jjr
Copy link
Copy Markdown

jason-rogers-jjr commented Sep 13, 2017

<html>
    <head>

<!--[if lte IE 8]><script src="js/html5shiv.js"></script><![endif]-->
<script src="js/jquery.min.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/skel-layers.min.js"></script>
<script src="js/init.js"></script>

<noscript>
    <link rel="stylesheet" href="css/skel.css" />
    <link rel="stylesheet" href="css/style.css" />
    <link rel="stylesheet" href="css/style-xlarge.css" />
</noscript>

<style type="text/css">
  .no-fouc {display: none;}
</style>

<script type="text/javascript">
  document.documentElement.className = 'no-fouc';
 $(document).ready(function() {
    $('.no-fouc').removeClass('no-fouc');
 });    
</script>


   </head>

Where am I going wrong here? This results in the page not loading at all. Without it the page loads after about 1-2 seconds of FOUC.

Thanks

Jason

@yphil-dev
Copy link
Copy Markdown

@jason This "solution" does not work ; don't bother.

@brianbrownton
Copy link
Copy Markdown

@jason-rogers-jjr your script block is still in the head. It needs to be just before your closing body tag (eg. </body>)

@logichub
Copy link
Copy Markdown

logichub commented May 1, 2018

Working great! Thank you!

@rruprai
Copy link
Copy Markdown

rruprai commented Feb 15, 2019

@johnpolacek have you taken the time to determine how this impacts the page's SEO? Displaying the content as none will hide the content also from search engines. When using fetch as Google, it was showing a blank page. I understand the need to hide the content and use JS to remove the class.

@wellington1993
Copy link
Copy Markdown

Nice! 👍 Thanks.

@rendrap
Copy link
Copy Markdown

rendrap commented Nov 8, 2019

Woow, simply the best one I've ever tried. My use case is inline critical CSS not fully capture above the fold layout state and would flash upon the main CSS loads. Thank you for this.

@fazeelanizam13
Copy link
Copy Markdown

This doesn't work for external style sheets, does it? Since ready() doesn't care for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment