Reputation: 769
I'm not seeing this issue in any other browser that I've tested - IE, Chrome, Opera - but whenever I load a page from the server, I'm seeing a flash of unstyled content before the CSS is applied.
This is even happening on subsequent page loads where everything should be cached - every time the page loads I see the unstyled content for a split-second, then everything settles in.
It's also worth noting (perhaps?) that the page is using @font-face to pull some Google fonts. They are stored in a separate stylesheet being pulled after the main responsive stylesheets and media queries.
I've tried a few different things, to no effect:
One other thing that may be worth mentioning is that I used quite a lot of Element Type CSS selectors in the page's CSS. Is it possible that this is slowing down the rendering process?
This seems unlikely as there is no problem immediately re-rendering the page upon changing the dimensions of the window - the responsive stuff renders fine immediately.
So this leads me to believe that there is some issue with how the CSS is being loaded.
Here is my HEAD code:
<!DOCTYPE html>
<head>
<!--<meta name="robots" content="noindex" />-->
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi" />
<title></title>
<!-- responsive stylesheets -->
<link rel="stylesheet" href="resources/css/320.css" type="text/css" media="screen and (max-width:320px)" />
<link rel="stylesheet" href="resources/css/480.css" type="text/css" media="screen and (min-width:321px) and (max-width:480px)" />
<link rel="stylesheet" href="resources/css/768.css" type="text/css" media="screen and (min-width:481px) and (max-width:768px)" />
<link rel="stylesheet" href="resources/css/960.css" type="text/css" media="screen and (min-width:769px) and (max-width:960px)" />
<link rel="stylesheet" href="resources/css/960+.css" type="text/css" media="screen and (min-width:961px)" />
<!-- custom fonts stylesheet -->
<link rel="stylesheet" href="resources/css/fonts.css" type="text/css" />
<!-- favicon -->
<link rel="shortcut icon" href="resources/images/ui/favicon.ico">
<!--[if lt IE 9]>
<link rel="stylesheet" href="resources/css/960+.css" type="text/css"/>
<![endif]-->
</head>
WTF is going wrong with Firefox? It's driving me nuts!
Upvotes: 76
Views: 72125
Reputation: 497
Tried the other answers, but none is working for code below
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
</body>
</html>
Firefox still showing FOUC warning. The solution that works is by moving the <style />
to the bottom of <body />
. Hope this helps.
My solution for the FOUC warning :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<link rel="stylesheet" href="style.css" />
</body>
</html>
Upvotes: 0
Reputation: 195
I kept encountering this during Next.js app development when my Firefox DevTools were open, but when I closed the DevTools sidebar it stopped appearing. I'm still getting the warning in the console, however:
Layout was forced before the page was fully loaded. If stylesheets are not yet loaded this may cause a flash of unstyled content.
So something in the developer tools themselves are making this happen.
Upvotes: 1
Reputation: 359
I started getting this warning message even for new projects on Firefox:
Layout was forced before the page was fully loaded. If stylesheets are not yet loaded this may cause a flash of unstyled content.
Simply opening the Firefox About dialog and forcing it to update to the latest version (131 in my case) seems to have made those problems go away.
I no longer see the warnings after the Firefox update and restart. Perhaps it was just in a corrupted state and didn't actually need the new version but the update seems to have properly reinitialized it.
Upvotes: 0
Reputation: 11
My issue was caused by importing my CSS file in my JS file. Solved this by linking my CSS file in the HTML.
This makes the browser wait for the CSS to load before rendering content, which doesn't happen when CSS is dynamically loaded from a JS import.
Upvotes: 0
Reputation: 48
ok, there is a full working and easy solution.
devtools
, disable warnings
, and..: DONE.Maybe my answer look somehow funny, but i explain myself.
This warning is there for a reason and it has to do with rendering of a html
page.
Since html5
i never: 1) use head
tag, 2) use body
tag, 3) never close the html
tag. I don't say this is the right way, but is what i do and what i will continue to do. No matter this, in devtools
inspector
, FF have separate my code
and add both HEAD
and BODY
tags. So FF pasre -> categorize the source -> organize and try to optimize the loading of a page.
In short when FF sees an not link
,script
,meta
tag, starts the BODY
parsing. if later you have some css code that may alter the view of that BODY
above, then that, maybe flush your view... Didn't you know that ? Of course you already know this! That's what the warning say
So, if want not to see the warning, design your code more right
and think like a browser. Also remember that
javasrcipt
and rendering is a mostly synchronous process.write
code thinking as above, fetching resourses may also cause the warning to show.So, am i the clever one that design everything right and not see this ? Of course not. Otherwise never search and land here.
Upvotes: -1
Reputation: 33
In my case (AWS Amplify), I was using a 302 redirect from https://website.com to https://www.website.com. Switching to a 301 redirect fixed this.
The problem could be a cache mixup for the background image on my site, since the bottleneck was some hundred ms of latency to download it.
Upvotes: 0
Reputation: 392
I had the same problem (but also in chrome). Even if many of the existing answers provide clues to the reason for FOUC I wanted to present my problem and its solution.
As I said, I had FOUC in a fairly large project and already had the suspicion of a racecondition in some form.
In the project SASS is used and via a "bootstrap" file for the css a fontawesome free package was added via import.
@import "@fortawesome/fontawesome-free/css/all.css";
This import has increased the total size of the css file by a lot, which caused the file to take a long time to load, and the browser went and already loaded the following javascript. The JS that was then executed forced the rendering of its content and thus created the FOUC.
So the solution in my case was to remove the big fontawesome package and insert the icons I used from it (~10) via an Icomoon custom font. Not only did this solve the FOUC but it also had the nice side effect that the delivered CSS files are much smaller.
Upvotes: 0
Reputation: 447
For what it's worth, I had this same problem and found that it was being caused by having poorly formatted <html>...</html>
tags.
To be precise, in my code I accidentally closed the HTML tag too early, like this:
<!DOCTYPE html>
<html lang="en"></html>
<head>
<title>My title</title>
The code provided by the original poster is missing the opening <html>
so I suspect that's probably what is happening there.
Upvotes: 11
Reputation: 1370
I had the same problem with Layout was forced before the page was fully loaded. If stylesheets are not yet loaded this may cause a flash of unstyled content.
showing in the console, and a visible flash of unstyled content upon page refresh, withouth (F5) or with clearing the cache (Ctrl + F5). Having the developer tools open does not made a difference either.
What helped me was declaring a variable in a script just before the </head>
tag ended, so basically after all the <link>
tags.
It's important to note, that an empty script (or with just a comment) or any random javaScript would not help, but declaring a variable worked.
<head>
<link rel="stylesheet" href="css/main.css" />
<link rel="stylesheet" href="css/other.css" />
<script>
/*to prevent Firefox FOUC, this must be here*/
let FF_FOUC_FIX;
</script>
</head>
There was no need to rearrange links or not use imports within css or js files.
Please note that the issue will no longer be visible (FOUC is visibly gone), but the console might still show the same warning.
Upvotes: 46
Reputation: 699
If you add a dummy <script>
tag right after <body>
, Firefox will show the page after all the css from <head>
is loaded:
<body>
<script>0</script>
<!-- rest of the code -->
</body>
There is an official bugreport about this FOUC (Flash Of Unstyled Content) on the Firefox site: https://bugzilla.mozilla.org/show_bug.cgi?id=1404468
Upvotes: 66
Reputation: 455
In my case the reason of FOUC in FF was the presence of iframe on page. If I removing iframe from markup then FOUC disappears.
But I need iframe for my own hacking reasons so I changed this
<iframe name="hidden-iframe" style="display: none;position:absolute;"></iframe>
into this
<script>
document.addEventListener('DOMContentLoaded', ()=>{
let nBody = document.querySelector('body')
let nIframe = document.createElement('iframe');
nIframe.setAttribute('name', "hidden-iframe");
nIframe.style.display = 'none';
nIframe.style.position = 'absolute';
nBody.appendChild(nIframe);
});
</script>
I've added this inline JS right in template just for readability: in my case this code runs once per page. I know that it's dirty hack, so you can add this code in separated JS-file.
The problem was in Firefox Quantum v65.
Upvotes: 4
Reputation: 30565
I was experiencing this error. A colleague has said that it's caused by the attribute, autofocus
being added to a form field.
By removing this attribute and using JavaScript to set the focus the brief flash of unstyled content stops happening.
Upvotes: 15
Reputation: 79
I've had the same issue. In my case removing @import
rule in the CSS file and linking all the CSS files in the HTML resolved it.
Upvotes: 7
Reputation: 4496
Filament Group share they way they load their fonts in detail,
http://www.filamentgroup.com/lab/font-loading.html
which is a nice modern approach to @font-face loading
Smashing magazine also review there website performance and came up with a different solution that stores the caches a base64 copy of the font in local storage. This solution may require a different licence for you font.
A gist can be found at:
https://gist.github.com/hdragomir/8f00ce2581795fd7b1b7
The original article detailing their decision can be fount at:
The head of your document contains far to many individual stylesheets, all these css files should be combined into a single file, minified and gziped. You may have a second link for your fonts placed before you main stylesheet.
<link rel="stylesheet" href="resources/css/fonts.css" type="text/css" />
<link rel="stylesheet" href="resources/css/main.css" type="text/css" />
Upvotes: 5