htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Dynamically load CSS

Open benpate opened this issue 4 years ago • 1 comments

Currently, htmx parses <script> tags in a dynamically loaded HTML fragment, but it does not do the same for stylesheets. It would be great if we could make this work:

index.html

<script src="https://unpkg.com/htmx.org"></script>
<div hx-get="dynamic.html" hx-trigger="load"></div>

dynamic.html

<!-- Either load linked stylesheets -->
<link rel="stylesheet" href="test.css" />

<!-- Or parse inline styles -->
<style>
    .red {color:red;}
</style>

<b class="red">This is Red</b>
<b class="blue">This is Blue</b>

benpate avatar Apr 17 '21 19:04 benpate

I'm using HTMX for the first time, and I've observed that both inline styles and stylesheets in the HTML fragment are parsed as long as the <link> tag isn't the first tag in the fragment. When the <link> tag is first, no styles are parsed. When I start the fragment with even an empty tag like <span></span> or <script></script>, then styles are parsed.

dlh01 avatar Dec 20 '23 10:12 dlh01