Description: The x-cloak directive in AlpineJS is used to hide Alpine-controlled elements until AlpineJS has fully initialized and applied its logic to them. This prevents a "flash of unstyled content" (FOUC), where users might briefly see pre-render states, raw template markup, or elements that should be initially hidden according to Alpine's logic.
x-cloak attribute:
This is a directive you add to HTML elements that you want to hide until AlpineJS has finished initializing them. AlpineJS automatically removes this attribute from the element once it has processed the component that element belongs to. By itself, the attribute does nothing; it relies on a corresponding CSS rule.
[x-cloak] { display: none !important; }
This CSS rule is essential. It targets any element with the x-cloak attribute and hides it using display: none. The !important flag is used to ensure this rule has high specificity and isn't easily overridden by other styles, which is crucial for reliably preventing FOUC. You must include this CSS in your project, either in a global stylesheet or a <style> tag in your HTML's <head>.
[x-cloak]:
The x-cloak attribute is merely a hook. Without the CSS rule [x-cloak] { display: none !important; }, the attribute has no effect, and elements will still flash. AlpineJS only removes the attribute; it doesn't inherently hide the element before removal.
x-cloak unnecessarily to static elements:
x-cloak is intended for elements whose initial display state or content is managed or significantly altered by AlpineJS (e.g., using x-show, x-if, x-text). Applying it to purely static HTML content that doesn't change upon Alpine's initialization offers no benefit and might very slightly delay its rendering as the browser waits for Alpine to remove the attribute.
x-cloak CSS with less specific or conflicting rules:
The !important in display: none !important; helps prevent this, but it's still possible if you have other very specific CSS rules or inline styles that might conflict. If elements are still flashing despite using x-cloak and its CSS, inspect the element in your browser's developer tools to see what styles are being applied and ensure your [x-cloak] rule is active and not overridden before Alpine initializes.
This example demonstrates the 'Flash of Unstyled Content' (FOUC) and how x-cloak prevents it.
We simulate a small delay (1 second) before AlpineJS fully initializes the content below to make any FOUC more apparent.
x-cloak (FOUC May Be Visible)Observe the content below. The orange box with the message "This should be hidden by Alpine..." might briefly appear and then disappear. This is FOUC.
shouldShowTemporaryMessage is false). If you see this, it's FOUC.
Dynamic content loaded!
Status:
x-cloak (FOUC Prevented)Here, the same orange box is correctly hidden from the start by x-cloak, preventing any flash.
x-cloak ensures it is from the very start). You should NOT see this.
Dynamic content loaded! (smoothly)
Status:
Note: To best re-observe FOUC on the "Without x-cloak" side after a reset, a full page refresh (Ctrl/Cmd+R) is recommended.
Alpine's reactivity and browser caching might make FOUC less apparent on subsequent resets without a full refresh.
The x-cloak side will always remain smooth.