Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Font Awesome icons vanish and the console shows CSP violations

ErrorRefused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'".

Short answer

Font Awesome's own troubleshooting page says Kits are not compatible with Content Security Policy. If your site sends a strict policy, switch to the self-hosted or npm setup, turn off automatic CSS insertion, and link the stylesheet yourself. That keeps the policy intact without opening it to extra hosts.

  • Applies to Font Awesome 7 (self-hosted and npm), Chrome current release, Firefox current release
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the console, note which directive each violation names, and print the policy the server sends. Then check how Font Awesome is loaded: a Kit script, a self-hosted file or an npm package.

    curl -sI https://example.com/ | grep -i "content-security-policy"
    curl -s https://example.com/ | grep -iE "kit.fontawesome|fontawesome"
  2. 2Match what you see, then jump to its fix

    1. 1The page loads a script from kit.fontawesome.com and the icons fail only when the policy is on.A Kit is used on a site that sends a CSPFix
    2. 2A style-src violation appears although the script file loads.Self-hosted JavaScript inserts its CSS into the pageFix
    3. 3A React, Vue or bundled app shows the same style-src violation.The npm svg-core package inserts its CSS at runtimeFix

What does the error look like?

  • Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'".
  • Refused to load the script 'https://kit.fontawesome.com/your-kit-id.js' because it violates the following Content Security Policy directive: "script-src 'self'".

Cause 1 of 3

A Kit is used on a site that sends a CSP

1Check

The Font Awesome troubleshooting page says that if a Kit is not working you should make sure you are not using Subresource Integrity or Content Security Policy, because these features are not compatible with Kits.

curl -s https://example.com/ | grep -o "https://kit.fontawesome.com/[^\"]*"

2Fix

Stop using the Kit script on that site. Self-host the files, or install the npm package, and follow the settings in the next cause.

<!-- remove -->
<script src="https://kit.fontawesome.com/your-kit-id.js"></script>

3Verify

Reload with the policy on. No request to kit.fontawesome.com should remain.

curl -s https://example.com/ | grep -c "kit.fontawesome.com"

Confirmed by docs.fontawesome.com

Did this fix it?

Cause 2 of 3

Self-hosted JavaScript inserts its CSS into the page

1Check

The Font Awesome security page says the default setup will violate a strict CSP because the library tries to insert CSS into the document head.

curl -sI https://example.com/ | grep -i "content-security-policy" | tr ';' '\n' | grep -i "style-src"

2Fix

For the self-hosted all.js, set data-auto-add-css to false on the script tag, and link the CSS file yourself.

<script src="/fontawesome/js/all.js" data-auto-add-css="false"></script>
<link href="/fontawesome/css/svg-with-js.css" rel="stylesheet" />

3Verify

Reload. The style-src violation should be gone and the icons should render.

curl -sI https://example.com/fontawesome/css/svg-with-js.css | head -n 1

Confirmed by docs.fontawesome.com

Did this fix it?

Cause 3 of 3

The npm svg-core package inserts its CSS at runtime

1Check

The same security page documents the autoAddCss setting for the fontawesome-svg-core package, and notes the CSS file lives at node_modules/@fortawesome/fontawesome-svg-core/styles.css.

grep -rn "fontawesome-svg-core" package.json

2Fix

Set config.autoAddCss to false before any other Font Awesome call, and import the stylesheet as a normal file.

import { config } from '@fortawesome/fontawesome-svg-core'
import '@fortawesome/fontawesome-svg-core/styles.css'

config.autoAddCss = false

3Verify

Rebuild, reload with the policy on, and check that the console has no new violation.

npm run build

Confirmed by docs.fontawesome.com

Did this fix it?

How do I stop it coming back?

  • Decide at the start whether the site needs a CSP; if it does, do not use a Kit.
  • Self-host icons or use the npm package, and load their CSS as a file.
  • Test policy changes in Content-Security-Policy-Report-Only before they are enforced.
  • Pin the Font Awesome version you self-host, so a file path does not change under you.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.