Skip to main content

How to style contents in dangerouslySetInnerHTML with Tailwind

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.

People found this article searching for

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

More to read

How to bill Apple for App Store payouts (and keep your accountant happy)

Apple hands you a CSV, not an invoice. Drop your App Store Connect financial report below and instantly generate the invoice, payout voucher, and bookkeeping cover sheet your accountant needs — entirely in your browser.

How to check if an iOS app is installed in Expo and React Native

Use Linking.canOpenURL to detect if another app is installed on iOS in Expo. Covers Expo config plugin setup, URL scheme configuration, CNG workflow, and a hook to conditionally show a deep link button.