<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mark</title>
    <description>The latest articles on DEV Community by Mark (@markmercier).</description>
    <link>https://dev.to/markmercier</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F252200%2Fbd062536-1c62-4b37-b779-6aa096a69219.jpeg</url>
      <title>DEV Community: Mark</title>
      <link>https://dev.to/markmercier</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/markmercier"/>
    <language>en</language>
    <item>
      <title>Combatting Flashes of Unstyled Content</title>
      <dc:creator>Mark</dc:creator>
      <pubDate>Thu, 20 Feb 2020 14:49:02 +0000</pubDate>
      <link>https://dev.to/markmercier/combatting-flashes-of-unstyled-content-k31</link>
      <guid>https://dev.to/markmercier/combatting-flashes-of-unstyled-content-k31</guid>
      <description>&lt;p&gt;I've been noticing issues with flashes of unstyled content lately, and wanted to find a go-to solution to build into all my future sites. &lt;/p&gt;

&lt;p&gt;After some Googling, I found &lt;a href="https://petrey.co/2017/05/the-most-effective-way-to-avoid-the-fouc/"&gt;this article by Stephen Petrey&lt;/a&gt; that presents a couple of effective solutions:&lt;/p&gt;

&lt;h3&gt;
  
  
  The Javascript Approach
&lt;/h3&gt;

&lt;p&gt;The Javascript-based approach goes something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load the page with a &lt;code&gt;no-js&lt;/code&gt; class on the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element

&lt;ul&gt;
&lt;li&gt;Use CSS to hide the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Replace the &lt;code&gt;no-js&lt;/code&gt; class with a &lt;code&gt;js&lt;/code&gt; class using Javascript

&lt;ul&gt;
&lt;li&gt;Use CSS to make the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element visible again once the &lt;code&gt;js&lt;/code&gt; class is added&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The downside, as Stephen mentions, is that if Javascript is disabled, the page will load but be invisible. For many sites, this may be perfectly acceptable, but I don't love the idea of the page being invisible to no-JS visitors. On to the next one!&lt;/p&gt;

&lt;h3&gt;
  
  
  The CSS Approach
&lt;/h3&gt;

&lt;p&gt;A clever comment on that article by electrotype provides a CSS-only solution. The gist is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inline CSS styles into the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; to hide the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element&lt;/li&gt;
&lt;li&gt;Add styles to the end of your last CSS file to reverse those styles and make the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element visible again&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is much more my style - once all of your CSS styles are parsed, the site will become visible without any flashes, all without Javascript!&lt;/p&gt;

&lt;p&gt;However, there's still a weakness - if your CSS fails to load, the site will still be invisible. That's probably pretty unlikely, but it still doesn't sit well for me to depend on that file loading successfully for the site to be visible.&lt;/p&gt;




&lt;h1&gt;
  
  
  My Modified CSS Approach
&lt;/h1&gt;

&lt;p&gt;My ideal solution combines the CSS-only approach with a fallback in case the stylesheet fails to load.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;CSS Animation to the rescue!&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;You'll need to add this to the top of the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fouc-fix&lt;/span&gt; &lt;span class="m"&gt;5s&lt;/span&gt; &lt;span class="n"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="n"&gt;fouc-fix&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this to the end of your final CSS file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt; &lt;span class="cp"&gt;!important&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break that all down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Immediately upon loading the &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tag in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;, the &lt;code&gt;fouc-fix&lt;/code&gt; animation begins on the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element, setting its opacity to 0.&lt;/li&gt;
&lt;li&gt;Once the CSS file is fully parsed, the animation is forced to &lt;code&gt;none&lt;/code&gt;, immediately resetting the opacity to default (1).&lt;/li&gt;
&lt;li&gt;If the CSS file fails to load, the animation will complete itself after 5 seconds, setting the opacity back to 1.

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;The &lt;code&gt;step(1)&lt;/code&gt; parameter makes the animation jump straight to the final state, rather than changing gradually.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You could set the animation time to whatever you want - 5 seconds seemed like a good amount of time to wait for the CSS to load.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Let me know in the comments if there's anything I missed or if you have a solution that works even better!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>html</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
