Skip to main content

How to style contents in dangerouslySetInnerHTML with Tailwind

How to style HTML content in React with Tailwind CSS when using dangerouslySetInnerHTML by writing custom selector using arbitrary variants.

2 min read
How to style contents in dangerouslySetInnerHTML with Tailwind

I recently needed to style the Html content that was injected into an element using the dangerouslySetInnerHTML property. Since the other parts of the project were using Tailwind, I wanted to continue the same approach here instead of writing custom CSS for that part. Turns out Tailwind makes this relatively easy using the arbitrary variants. This allowed me to do the following:

const Tooltip = ({ content }: { content: string }) => (
    <div className="bg-white">
        <div className="[&>p]:text-zinc-800"
            dangerouslySetInnerHTML={{ __html: content }}
        />
    </div>
)

Essentially you can target any element using the [&>element] syntax. Tailwind itself for example has an example of [&:nth-child(3)], and in case you need to style for example all the p elements in the dangerouslySetInnerHTML, not just the direct descendants as in the example above, you can use underscore: &_p as replacement for space in your selector.


Comments

No comments yet. Be the first to share your thoughts.

Leave a comment

Comments are moderated before they appear.

More to read

How not to ship slop

AI is useful, but it will happily help you ship average work with a finished-looking surface — here is how to keep it from setting your bar.

App.js 2026 recap

A recap of App.js 2026 with a sponsor's, speaker's, and participant's perspective.

People found this searching for

  • "[&_*]:" tailwind
  • "[&>*]:text" tailwind
  • "[&_*]" tailwind