Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                </code><h1>Testing out a <code>content-warning</code> attribute with better accessibility support</h1>

<h2>Example 1: Inline use, spoiler</h2>

<p>I thoroughly enjoyed the film, especially
when it was revealed that 
<span content-warning="spoiler">Taye Diggs
  had been the bad guy all along</span>.</p>

<h2>Example 2: Block use, sensitive topic</h2>

<article content-warning="child abuse">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, sapien vel bibendum bibendum, nunc sapien tincidunt velit, vel lacinia enim velit ac augue. Donec euismod libero auctor, aliquam elit quis, malesuada magna. Sed ut urna ac odio bibendum bibendum. Nulla facilisi. Donec euismod nisl vel ante malesuada, vel lacinia sapien ultricies.</p>
  <p>Suspendisse potenti. Sed at mauris in velit ornare tristique. Sed vitae urna eget nisl commodo malesuada. Nullam euismod massa non enim aliquet, at volutpat eros sagittis. Praesent id elit ut nibh eleifend luctus. Fusce euismod dui vel tellus blandit, sed vestibulum odio lobortis.</p>
  <p>Nullam sit amet libero vitae ipsum ultrices rhoncus a et nulla. Nullam eget metus in nisi commodo suscipit. Donec nec est quis quam fringilla ullamcorper ac sed dolor. Sed auctor semper mi, vel dictum justo tincidunt et. Donec eget tortor ut lorem faucibus malesuada.</p>
  <p>Sed euismod nibh quis erat ultrices, ut hendrerit risus bibendum. Aliquam erat volutpat. Donec id augue non magna gravida blandit vel id sapien. Sed vitae orci nec turpis feugiat aliquet eget vitae diam.</p>
</article>

<h2>Example 3: Image use, graphic imagery</h2>

<figure content-warning="violence gore">
  <img src="https://image.tmdb.org/t/p/original/jJKiJPDe7yWsxLNj9HWFx9UApsc.jpg"  width="400" alt="A screen capture form Army of Darkness">
</figure>  
              
            
!

CSS

              
                body {
  padding: 0 1rem;
}

figure {
  display: inline-block;
}

[content-warning] {
  visibility: hidden;
}

[content-warning][role=alert] {
  visibility: visible;
}

h2 {
  margin-block-start: 3em;
}
              
            
!

JS

              
                const $content_warnings = document.querySelectorAll('[content-warning]');

class ContentWarningElement extends HTMLElement {
  
  constructor() {
    // Always call super first in constructor
    super();
    
    const $shadow = this.attachShadow({ mode: "closed" });
    
    this.$wrapper = document.createElement("span");
    this.$wrapper.setAttribute("class", "wrapper");
    $shadow.appendChild(this.$wrapper);
    
    this.$button = document.createElement('button');
    this.$button.addEventListener( 'click', this.reveal.bind(this) );
    this.$wrapper.appendChild(this.$button);
    
    this.$original = null;
    
    let $style = document.createElement("style");
    $style.textContent = `
      .wrapper {
        background: #ebebeb;
        position: relative;
      }
      button {
        display: inline;
        text-align: center;
        cursor: pointer;
        align-items: center;
        justify-content: center;
        margin: 0;
      }
      button.block {
        display: block;
        position: absolute;
        inset: 0;
      }
    `;
    $shadow.appendChild($style);
    
  }
  
  swap( $el ){
    this.$original = $el;
    this.$parent = $el.parentNode;
    this.original_styles = window.getComputedStyle( $el );
    this.inheritStyles();
    this.positionButton();
    this.$button.innerText = `Content Warning: ${$el.getAttribute("content-warning")}. Click to reveal.`;
    this.hide($el);
  }
  
  inheritStyles(){
    // images and non-inline elements
    if ( this.$original.nodeName.toLowerCase() == 'img' ||
         this.original_styles.display != "inline" ) {
      ["display", "width", "height", "margin"].map(property=>{
        this.$wrapper.style[property] = this.original_styles[property];
      });
      if ( this.original_styles.margin == "0px" ) {
        this.$wrapper.style["margin-block-start"] = window.getComputedStyle( this.$original.firstElementChild )["margin-block-start"];
        this.$wrapper.style["margin-block-end"] = window.getComputedStyle( this.$original.firstElementChild )["margin-block-end"];
      }
    }
    // everything else
    else {
      this.$button.style.blockSize = `${this.$original.offsetHeight}px`;
      this.$button.style.inlineSize = `${this.$original.offsetWidth}px`;
    }
  }
  
  positionButton(){
    if ( this.$original.nodeName.toLowerCase() == 'img' ||
         this.original_styles.display != "inline" ) {
      this.$button.setAttribute("class", "button block");
    }
  }
  
  hide( $el ){
    this.$parent.replaceChild( this, this.$original );
  }
  
  reveal(){
    this.$original.setAttribute("role","alert");
    this.$parent.replaceChild( this.$original, this );
    focus(this.$original);
  }
}

customElements.define("content-warning", ContentWarningElement);

window.addEventListener("load",function(){
  $content_warnings.forEach($cw => {
    let $cwElement = document.createElement('content-warning');
    $cwElement.swap($cw);
  });
}, false);
              
            
!
999px

Console