How do I stop the flash of unstyled content (FOUC) on a web page?
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
An other quick fix which also works in Firefox Quantum is an empty
<script>
tag in the<head>
. This however, penalizes your pagespeed insights and overall load time.I had 100% success with it. I think it's also the main reason, why above solutions with other JS in the works.
This is the one that has worked for me and does not require javascript and it works great for pages with many elements and lots of css:
First, add a dedicated
<STYLE>
setting for the<HTML>
tag with visibility 'hidden' and opacity as '0' at the top of your HTML, e.g, in the beginning of the<HEAD>
element, for example, at the top of your HTML add:Then, at the end of your last .css stylesheet file, set the visibility and opacity styles to 'visible' and '1', respectively:
If you already have an existing style block for the 'html' tag, then move the entire 'html' style to the end of the last .css file and add the 'visibility' and 'opacity' tags as described above.
https://gist.github.com/electrotype/7960ddcc44bc4aea07a35603d1c41cb0
What I've done to avoid FOUC is:
Set the body section as:
<body style="visibility: hidden;" onload="js_Load()">
Write a
js_Load()
JavaScript function:document.body.style.visibility='visible';
With this approach the body of my web page is kept hidden until the full page and CSS files are loaded. Once everything is loaded, the onload event turns the body visible. So, the web browser remains empty until a point when everything pops up on the screen.
It is a simple solution but so far it is working.
A CSS-only solution:
As the browser parses through the HTML file:
<html>
.The advantage to this over a solution that uses JavaScript is that it will work for users even if they have JavaScript disabled.
Note: you are allowed to put
<link>
inside of<body>
. I do see it as a downside though, because it violates common practice. It would be nice if there was adefer
attribute for<link>
like there is for<script>
, because that would allow us to put it in the<head>
and still accomplish our goal.No one has talked about CSS
@import
That was the problem for me i was loading two extra style sheets directly in my css file with
@import
Simple solution: Replace all
@import
links with<link />
A solution which doesn't depend on jQuery, which will work on all current browsers and do nothing on old browsers, include the following in your head tag:
Thanks to @justastudent, I tried just setting
elm.style.display="none";
and it appears to work as desired, at least in current Firefox Quantum. So here is a more compact solution, being, so far, the simplest thing I've found that works.