Problem:
Loading a mobile website occasionally begins by displaying the page without CSS, causing a noticeable visual delay. The goal is to force browsers to prioritize CSS loading and rendering before displaying the content. However, methods involving placing CSS files outside the head are not recommended due to potential compatibility issues.
Solution:
A more effective approach involves introducing a temporary overlay div at the beginning of the output file:
<code class="html"><body> <div id="loadOverlay" style="background-color:#333; position:absolute; top:0px; left:0px; width:100%; height:100%; z-index:2000;"></div> ... </body></code>
Next, add the following code to the last line of the final CSS file:
<code class="css">#loadOverlay{display: none;}</code>
Explanation:
This method leverages the initial display issue to hide the page content using a full-screen div overlay. Once the necessary CSS files are loaded and processed, the last CSS code removes this overlay, revealing the correctly rendered page. This technique resolves the problem without introducing hacks that may affect browser compatibility.
The above is the detailed content of How to Force Browser CSS Rendering Before Page Display for a Mobile Website?. For more information, please follow other related articles on the PHP Chinese website!