Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash of unstyled content (FOUC) in Firefox when using 'autofocus' in input field

Actually, this is more of an error description than a question.

I noticed flash of unstyled content (FOUC) in Firefox on a very simple login page. There are no images used. No heavy CSS. All Javascript placed at the end of the code, right before the closing body tag. But when the page loads, Firefox shows a completely unstyled page for about 100 ms and then the css takes effect. This happens everytime, without exception.

Accidently I found some sort of a hack to solve this: Just add a <script> element to the <head>, place any javascript code you like in it or even a simple comment and - bam! - problem solved. Like this:

<script>/* foo */</script>

The actual reason for that FOUC seems to be the use of the autofocus attribute with one of the form fields. So removing 'autofocus' solves the problem too.

Isn't that weird? Does anyone know a better solution than mine?

like image 536
Christian Avatar asked Apr 29 '17 22:04

Christian


People also ask

What causes flash of unstyled content?

A flash of unstyled content (or flash of unstyled text) is an instance where a web page appears briefly with the browser's default styles prior to loading an external CSS stylesheet, due to the web browser engine rendering the page before all information is retrieved.

What is FOUC HTML?

FOUC stands for Flash of Unstyled Content. This situation occurs whenever a Web browser ends up showing your Web page's content without having any style information yet.


1 Answers

I can confirm this behaviour in Firefox v.53, by elimination I tracked it down to the 'autofocus' attribute on an text input too.

You can remove the FOUC by setting the focus in Javascript instead, something like:

document.getElementsByClassName('form-field')[0].focus();

Or with jQuery:

$('.form-field').focus();
like image 96
Nick Bergquist Avatar answered Sep 18 '22 13:09

Nick Bergquist