Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <h1>Spoilers</h1>
A few hoverable, clickable, tabbable spoiler revealers.

<h2>CSS Hover Spoiler / Text Spoiler</h2>
<p>
  A pure CSS spoiler revealer that is <span class="spoiler-text" tabindex="0">clickable, tabbable, and
    hoverable</span>
  <br />
  Lorem ipsum dolor, sit amet consectetur adipisicing elit. Ipsum <span class="spoiler-text" tabindex="0">blanditiis molestiae eligendi</span> non. Ullam doloribus quibusdam at facilis atque! Dolorum praesentium eveniet dicta ullam, aperiam dignissimos voluptate incidunt enim maiores.
</p>

<h2>HTML Details Tag</h2>
<details>
  Pure HTML without any Styling, notice how the text defaults to "Details" when we don't provide a <code>&lt;summary&gt;</code> tag.
</details>

<details class="spoiler-details">
  <summary>Answer Key</summary>
  <p>This is a styled <code>&lt;details&gt;</code> tag. Note that the open/close can not be animated/transitioned directly without hardcoding the height for example.</p>
  <ol>
    <li>A</li>
    <li>D</li>
    <li>C</li>
    <li>B</li>
    <li>C</li>
  </ol>
</details>

<details class="spoiler-details animated">
  <summary>Animated Details</summary>
  <p>This details block has an animated soft opacity "flash"</p>
  <div class="content">
    <span>You can also add more intricate animations such as slide-in effects (but you would probably avoid using a border in such cases)</span>
  </div>
</details>

<h2>JavaScript Spoiler</h2>
<p>The most flexible option but it requires some more work.</p>

<span class="js-spoiler hidden" aria-label="Spoiler" aria-expanded="false" tabindex="0" role="button">
  <span aria-hidden="true">Jar Jar Binks is a sith lord. Clicking this again will toggle the spoiler</span>
</span>

<br />

<span class="js-spoiler hidden" aria-label="Spoiler" aria-expanded="false" tabindex="0" role="button">
  <span aria-hidden="true">Wilson doesn't survive... and now you can never close this spoiler</span>
</span>
              
            
!

CSS

              
                // for article purposes only
body {
  padding: 1rem 2rem;
}

.huge {
  font-size: 5rem;
}

///////////////////////////////////

.spoiler-text {
  background: black;
  color: transparent;
  cursor: help;
  user-select: none;

  transition: background 0.3s ease 0.2s, color 0.2s ease 0.25s;
}

.spoiler-text:hover,
.spoiler-text:focus {
  background: #e8e8e8;
  color: inherit;
}

.spoiler-details {
  border: 1px solid #bbb;
  border-radius: 5px;
  padding: 0.5rem;
  margin: 0.5rem;
  max-width: 50%;
  min-width: 300px;

  // trying to transition height won't work unless you set a fixed height
  // transition: height 1s ease;
}

.spoiler-details summary {
  cursor: pointer;
  font-weight: bold;
  list-style: none;
  padding: 0.25rem;
}

.spoiler-details[open] summary {
  border-bottom: 1px solid #bbb;
}

.spoiler-details.animated[open] {
  animation: flash 0.5s ease-in-out;
}

.spoiler-details.animated[open] .content {
  opacity: 0;
  animation: slide 0.5s 1s ease-in-out;
  animation-fill-mode: forwards;
}

// a soft opacity flash to show the user that something happened
@keyframes flash {
  0% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}

// simple slide in
@keyframes slide {
  0% {
    margin-left: -50%;
    opacity: 0;
  }
  100% {
    margin-left: inherit;
    opacity: 1;
  }
}

.js-spoiler {
  background: #e8e8e8;
}

.js-spoiler.hidden {
  background: black;
  cursor: pointer;
  border-radius: 3px;
}

.js-spoiler.hidden span {
  opacity: 0;
  user-select: none;
}

              
            
!

JS

              
                function handleSpoiler(evt) {
  const wrapper = evt.currentTarget;
  const content = wrapper.children[0];

  // toggle the visibility
  wrapper.classList.toggle("hidden");

  // set aria attributes for screen readers
  if (wrapper.classList.contains("hidden")) {
    wrapper.setAttribute("aria-expanded", false);
    wrapper.setAttribute("role", "button");
    wrapper.setAttribute("aria-label", "spoiler");

    content.setAttribute("aria-hidden", true);
  } else {
    wrapper.setAttribute("aria-expanded", true);
    wrapper.setAttribute("role", "presentation");
    wrapper.removeAttribute("aria-label");

    content.setAttribute("aria-hidden", false);
  }
}

// an array of our js-spoilers
const jSpoilers = [...document.getElementsByClassName("js-spoiler")];

// hard coding two event listeners to demonstrate a togglable and a one time only spoiler
jSpoilers[0].addEventListener("click", handleSpoiler);
jSpoilers[1].addEventListener("click", handleSpoiler, { once: true });

              
            
!
999px

Console