Flash of unstyled text (FOUT) is an instance where a web page appears briefly with the browser’s default styles prior to loading an external CSS stylesheet
There is no way to avoid this issue since the network is unpredictable.
However, there are some ways to reduce the bad user experience !
We can use font-display
property to tell the browser how to handle this scenario. (by default is set to auto
)
Ex:
@font-face {
font-display: swap;
}
The font-display property accepts five values:
auto
(default): Allows the browser to use its default method for loading, which is most often similar to the block value.block
: Instructs the browser to briefly hide the text until the font has fully downloaded. More accurately, the browser draws the text with an invisible placeholder then swaps it with the custom font face as soon as it loads. This is also known as a “flash of invisible text” or FOIT.swap
: Instructs the browser to use the fallback font to display the text until the custom font has fully downloaded. This is also known as a “flash of unstyled text” or FOUT.fallback
: Acts as a compromise between the auto and swap values. The browser will hide the text for about 100ms and, if the font has not yet been downloaded, will use the fallback text. It will swap to the new font after it is downloaded, but only during a short swap period (probably 3 seconds).optional
: Like fallback, this value tells the browser to initially hide the text, then transition to a fallback font until the custom font is available to use. However, this value also allows the browser to determine whether the custom font is even used at all, using the user’s connection speed as a determining factor where slower connections are less likely to receive the custom font.