Flash of Unstyled Content (FOUC)


BlueRobot presents...

Flash of Unstyled Content (FOUC)

What is a FOUC?

Some pages that use the CSS @import rule experience a curious display quirk in the Windows version of MS Internet Explorer: a momentary flash of unstyled page content. The unimaginative creature that I am, I call this phenomenon Flash of Unstyled Content or FOUC for short.

Because this quirk seems to affect only MS Internet Explorer, we're tempted to conclude that IE is just plain wrong. I am not going to jump to that conclusion. It is unclear to me whether or not a FOUC afflicted user agent is in disagreement with any W3C recommendation. Though I can't speak for the W3C, I can say that Iwould prefer that Internet Explorer ditch the flash of unstyled content.

How do I see a FOUC?

First, you will need to be using IE for Windows (version 5 or above). Then, you'll need to find a page that exhibits the FOUC phenomenon. You won't need to search long - this very page¹ is a great example.

Now, this is very important. If the imported stylesheet is in IE's cache (also called Temporary Internet Files), the flash of unstyled content will not rear its ugly head. For testing purposes, delete IE's Temporary Internet Files before viewing the FOUC-inducing page. If you're not sure how to do this, please refer to Microsoft's brief online instructions. Below is my suggested method to test for a FOUC.

  1. Load the page that you want to test into an IE5+/Win browser window. This very page¹ is an excellent page to test.
  2. Load a different page into the browser window. The page that you chose for this step is not important, as long as it does not share files with the page that you are testing. A page from a different site is a good choice here (for example, the Google home page).
  3. Delete your Temporary Internet Files. If needed, instructions are available.
  4. Press the "Back" button to return to the page that you wish to test.
  5. Watch for the flash of unstyled content.

Please note that some factors, like a slow PC or proximity to network equipment with caching capabilities, may reduce the chance that you'll witness the flash of unstyled content display quirk. For example, I have a 100MHz Pentium PC running IE5/Win that will not exhibit this phenomenon for even the worst FOUC-afflicted pages.

What's the backstory?

When I first converted bluerobot.com to a CSS-based layout (18 FEB 2001), the design suffered from the FOUC phenomenon. It was immediately clear that this odd display quirk was somehow connected to the use of the CSS2 @import rule. Because the @import rule is widely used to hide complex CSS rules from CSS-impaired browsers (e.g. Netscape Navigator 4.x), the FOUC phenomenon has the potential to be more than just a small annoyance.

That said, I must admit that I've only recently given the FOUC issue more than a passing thought. The split-second rendering of raw content seemed somehow fitting on the BlueRobot home page. However, I've received too many emails regarding this issue to pretend that it's not a concern.

Is it all in my HEAD?

So, why do some pages (e.g. this page¹) exhibit the FOUC, while others (e.g. A List Apart) do not? Well, the ALA home page sure has more stuff inside its HEADelement than does this page. Of particular interest, the HEAD element on the ALA home page contains a couple of SCRIPT elements and a handful of LINK elements, while this page¹ contains no such elements. There's a solution in there somewhere.

How do I vanquish the FOUC? (How do I fix it?)

Just one LINK element or SCRIPT element inside a document's HEAD element will prevent a flash of unstyled content.

The basic HEAD element

For the purposes of comparison, below is the basic HEAD element that is susceptible to the FOUC phenomenon.

Test the basic HEAD element for the FOUC phenomenon.

<head>
<title>My Page</title>
<style type="text/css" media="screen">@import "style.css";</style>
</head>

The LINK element solution

The addition of a LINK element to the basic HEAD element is probably the most natural solution to the FOUC problem. This is because almost every page can benefit from the addition of either an alternate stylesheet or a media-dependent stylesheet. I recommend this solution, because it both prevents the FOUC display quirkand improves the page in some other way: either usability, accessibility, or some other fancy buzzword. Of course, this solution works even if the added LINK element refers to an empty stylesheet. But what a waste that would be.

Test the LINK element solution for the FOUC phenomenon.

<head>
<title>My Page</title>
<link rel="stylesheet" type="text/css" media="print" href="print.css">
<style type="text/css" media="screen">@import "style.css";</style>
</head>

The SCRIPT element solution

The addition of a SCRIPT element to the basic HEAD element is an effective solution to the FOUC problem. However, it may seem somewhat unnatural to add a SCRIPTelement to some pages. For example, I believe there is no script that would improve this page. I could add an empty SCRIPT element to the HEAD of this page, but that feels an awful lot like a hack. In cases like this, opt for the LINK element solution.

I should mention here that the SCRIPT element does not need to be inside the HEAD element to thwart the FOUC phenomenon. It does need to appear in the markup before all content contained by the BODY element. Therefore, placing a SCRIPT element inside the BODY element, but before all visual content, is also an effective way to prevent a FOUC.

Test the SCRIPT element solution for the FOUC phenomenon.

<head>
<title>My Page</title>
<script type="text/javascript"> </script>
<style type="text/css" media="screen">@import "style.css";</style>
</head>

How can I help?

If you know of any resources on the FOUC phenomenon, or have any related information for me, please let me know. There must be more information out there. I'd like to continue to improve our understanding of this pesky display quirk. And for heaven's sake, please let me know if you encounter any problems or errant information on this page. I appreciate any and all help.

Acknowledgements

I'd like to give a special thanks to Shawn Wall for sharing ideas, testing pages, and generally motivating me to get off my butt and write this document.

A Note

¹When no solution is in effect, this page exhibits the flash of unstyled content display quirk. However, some links on this page dynamically modify the page markup to demonstrate solutions to the FOUC problem. No FOUC solutions are currently in effect.


相关资源:
http://www.paulirish.com/2009/avoiding-the-fouc-v3/

关于如何测试这种现象:
在ie浏览器中打开 http://www.bluerobot.com/web/css/fouc.asp/
应该确保浏览器没有这个页面的缓存

个人总结:
1.这种情况出现在ie浏览器中;
2.之所以出现这种情况是因为在页面head中只使用了@import规则导入样式,而且没有其他任何的内嵌,外链的script,css;
3.解决方式就是在head中引入script或css,引入文件设置可以是没有内容的;
如果理解不当请指正;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值