SVGs
Inline vector graphics with reactive styling.
Setup
The SVG plugin is included in manifest.js with all core plugins, or can be selectively loaded.
<script src="https://cdn.jsdelivr.net/npm/mnfst@latest/lib/manifest.min.js"></script>
Use x-svg to load a local SVG file's <svg> element within it. Fetched files are cached in memory for the page session, similar to the markdown plugin.
File Content
Pass a path to an .svg file in the project directory. Apostrophes are optional for plain paths.
<div class="size-10" x-svg="'/assets/examples/icon-star.svg'"></div>
Inline Content
The SVG tag itself can be applied within apostrophes, using " for its internal double quotes. However this is redundant when the SVG is more easily placed directly as inline HTML.
<div class="size-10" x-svg="'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2l2.91 6.41L22 9.27l-5 4.87L18.18 22 12 18.77 5.82 22 7 14.14 2 9.27l7.09-.86z"/></svg>'"></div>
Dynamic Content
Alpine expressions can supply the SVG string or path at runtime (including template literals), same as dynamic markdown.
<div class="size-10" x-data="{ star: '<svg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'0 0 24 24\' fill=\'currentColor\'><path d=\'M12 2l2.91 6.41L22 9.27l-5 4.87L18.18 22 12 18.77 5.82 22 7 14.14 2 9.27l7.09-.86z\'/></svg>' }">
<span class="block size-full" x-svg="star"></span>
</div>
Data Sources
The SVG file's path can also be populated from a data source.
<span class="size-10 block" x-svg="$x.example.sampleSvg"></span>
Styles
Modify an SVG's appearance either within the SVG file itself, or using CSS on the host or an ancestor. Removing fixed width and height on the root <svg> (keeping viewBox) lets utilities or layout on the parent control size. Use fill="currentColor" or stroke="currentColor" so color on the wrapper flows into the graphic.
<div class="size-12 text-brand-content svg-fill" x-svg="'/assets/examples/icon-star.svg'"></div>
Untrusted Content
x-svg injects SVG markup as-is, including any scripts and event handlers it contains. Add the .safe modifier when the SVG comes from users or third parties. It sanitizes the markup with DOMPurify, loaded on first use, removing scripts, on* handlers and <foreignObject> content.
<div class="size-10" x-svg.safe="$x.uploads.avatarSvg"></div>
Article does not exist
There is no documentation at this path.