Web component / Examples

<content-warning>

A web component for block and inline content warnings. See the README for installation and API documentation.

Most examples author hidden so warning content stays hidden before upgrade and without JavaScript. The inline examples retain visible native text instead. Once loaded, the component adds a native reveal button. Reader Mode tools may still extract content; this isn’t an access-control mechanism.

Block Content Warning

A block-level content warning that overlays the entire content area. This page uses shared default wording; see Shared Labels to reuse a template across warnings.

Single Warning Type

Wrap the content and set type to name the warning. Tab to its button, then press Enter or Space to reveal it. Without JavaScript, authored hidden keeps this content hidden.

<content-warning type="violence" hidden>
  <p>This content contains graphic violence.</p>
  <p>The scene describes a battle and its aftermath.</p>
</content-warning>

Multiple Warning Types

Use space-separated types for multiple warning categories.

<content-warning type="violence spoilers nsfw" hidden>
  <p>Contains violence, spoilers, and mature content.</p>
  <p>The protagonist dies in the final battle, sacrificing themselves to save their friends.</p>
</content-warning>

Inline Content Warning

An inline content warning that works within text flow.

Inline Usage

Add inline to place the button within a sentence. This example omits hidden, so “episode 5” remains visible without JavaScript. Add hidden if that fallback would expose content you need to conceal.

<p>
  The character dies in
  <content-warning type="spoilers" inline>
    episode 5
  </content-warning>.
</p>

The character dies in episode 5.

Multiple Inline Warnings

You can have multiple inline warnings in one sentence.

<p>
  <content-warning type="spoilers" inline>Jon</content-warning>
  meets <content-warning type="spoilers" inline>Daenerys</content-warning>
  in season 7.
</p>

Jon meets Daenerys in season 7.

Content Hiding Modes

The component offers two modes for hiding content, each with different trade-offs.

Default Mode (Dark Overlay)

After render, the component applies hidden and inert to its content wrapper. inert prevents interaction and excludes the content from the accessibility tree. Block content keeps its layout beneath a dark, translucent overlay because the wrapper’s CSS overrides native hidden display. Authored hidden hides the whole element before upgrade; Reader Mode behavior still varies across tools.

<content-warning type="sensitive content" hidden>
  <p>The overlay covers this content until you reveal it.</p>
</content-warning>

Blur Mode (Visual Only)

Content is visually obscured with CSS blur but remains in the DOM. The wrapper uses aria-hidden="true" to exclude it from the accessibility tree, not inert. Avoid focusable descendants in blur examples. Bare text nodes aren’t blurred; inline warnings hide their wrapper instead. Not guaranteed to be hidden from Reader Mode.

<content-warning type="spoilers" blur hidden>
  <p>This content is blurred visually and may be extracted by Reader Mode.</p>
</content-warning>

Custom Blur Amount

Adjust the blur intensity using the --content-warning-blur-amount CSS custom property.

<style>
  .light-blur {
    --content-warning-blur-amount: 5px;
  }
  .heavy-blur {
    --content-warning-blur-amount: 20px;
  }
</style>

<content-warning type="spoilers" blur class="light-blur" hidden>
  <p>Lightly blurred content (5px blur)</p>
  <p>Text is more readable through the blur.</p>
</content-warning>

<br><br>
<content-warning type="spoilers" blur class="heavy-blur" hidden>
  <p>Heavily blurred content (20px blur)</p>
  <p>Text is much harder to read through the blur.</p>
</content-warning>


Comparison

Compare the dark overlay with visual blur. Both block warnings keep the paragraphs’ layout before reveal; blur lets their shapes show around the button.

<content-warning type="test" hidden>
  <p>Content covered by the overlay.</p>
</content-warning>

<content-warning type="test" blur hidden>
  <p>Visually blurred content.</p>
</content-warning>

Default (Overlay)

Blur Mode

Custom Styling

Customize the appearance using CSS Shadow Parts.

Custom Colors

Set both button colors to keep the label on an opaque backplate. The overlay can have a separate background; keep the focus outline visible.

<style>
  .custom-warning::part(overlay) {
    background: rgba(139, 0, 0, 0.95);
  }
  .custom-warning::part(button) {
    color: #fff;
    border: 3px solid #ff6b6b;
    padding: 2rem;
    background: #260000;
    border-radius: 0.5rem;
  }
</style>

<content-warning type="horror" class="custom-warning" hidden>
  <p>This is scary content with custom styling!</p>
  <p>The dark red background and red border make it stand out.</p>
</content-warning>

Styled Button Text

Style the button text and appearance.

<style>
  .styled-parts::part(button) {
    text-transform: uppercase;
    letter-spacing: 0.15em;
    font-weight: 900;
    background: #333;
    color: #fff;
    border-radius: 0.5rem;
  }
</style>

<content-warning type="mature" class="styled-parts" hidden>
  <p>This content is for mature audiences.</p>
</content-warning>

Event Handling

Listen for the revealed event to track user interactions.

Tracking Reveals

Activate the warning to update the status below it. The event fires once per reveal and supplies the authored type.

<content-warning type="spoilers" id="tracked" hidden>
  <p>This is the secret content that was revealed!</p>
</content-warning>
<p id="status">Status: Hidden</p>

<script>
  document.getElementById('tracked')
    .addEventListener('content-warning:revealed', (e) => {
      document.getElementById('status').textContent =
        `Status: Revealed (${e.detail.type})`;
    });
</script>

Status: Hidden

Labels and Translations

Write a complete button label with reveal-label. Its {type} token inserts the warning type wherever your sentence needs it. The visible text also names the button. Translate type too, and set lang on translated content. See the label reference for shared defaults, precedence, and recovery.

Use Your Application’s Vocabulary

Write the whole phrase to fit a review workflow, including its punctuation. This changes the label, not the reveal behavior.

<content-warning
  type="plot details"
  reveal-label="Review note: {type}. Show details"
  hidden>
  <p>The review discusses the ending.</p>
</content-warning>

Spanish Translation

The warning and image alternative inherit Spanish from their containing element. This photo is a placeholder, not a graphic image.

<content-warning
  type="violencia gore"
  reveal-label="Advertencia: {type}. Mostrar contenido"
  hidden
>
  <img src="https://picsum.photos/600/400?random=5"
    alt="Fotografía de ejemplo" style="display: block; max-width: 100%">
</content-warning>

French Translation

The warning inherits French from its containing element. Style the complete label while the button keeps an opaque background.

<style>
  .french-warning::part(button) {
    background: #111;
    color: #fff;
  }
  .french-warning::part(label) {
    font-weight: bold;
    color: #ffeb3b;
  }
</style>

<content-warning
  type="contenu sensible"
  reveal-label="Afficher le contenu : {type}"
  class="french-warning"
  hidden
>
  <p>Ce contenu pourrait être considéré comme sensible par certains utilisateurs. Veuillez faire preuve de discrétion.</p>
</content-warning>

A Shorter Label

Leave extra instructions out of your template when a short label says enough.

<content-warning
  type="graphic content"
  reveal-label="Reveal {type}"
  hidden
>
  <p>This warning uses a short reveal label.</p>
</content-warning>

Shared Labels

An inline JSON script supplies defaults for every warning on this page. The first example below uses that wording; the second overrides it with its own reveal-label. Replace the shared text with your application’s vocabulary or translation. Editing the JSON later requires refresh() on each initialized warning; it doesn’t reset revealed content.

<script type="application/json" data-content-warning-config>
  {
    "reveal-label": "Content Warning: {type} Click to reveal",
    "type-label": "content"
  }
</script>

<content-warning type="plot details" hidden>
  <p>The final scene introduces another narrator.</p>
</content-warning>

<content-warning
  type="spoilers"
  reveal-label="Read the ending: {type}"
  hidden>
  <p>The narrator returns home.</p>
</content-warning>

API Reference

For complete documentation of attributes, events, CSS custom properties, and browser support, see the README.