How to create spoiler text?

The <details> HTML tag was designed specifically for this purpose. Here is the example from the MDN docs. Unfortunately, IE and edge don't support this tag as of January 2019.

<details>
    <summary>Details</summary>
    Something small enough to escape casual notice.
</details>

Similar to spoiler in Disqus comments.

.spoiler{
  background-color: gray;
  color: transparent;
  user-select: none;
}

.spoiler:hover{
  background-color: inherit;
  color: inherit;
}
<h3>In the movie, <span class="spoiler">the hero kills his wife.</span></h3>

I have been spoilers on forums (including my own) that are not just text with the background color changed.

They have the content hidden until you click a show/hide toggle button.

I want to add a section to a site that doesn't show up by default to save space.

https://jsfiddle.net/clarle/bY7m4/

That seems like it will meet my needs.

HTML

<div class="forum-post">
    <a href="#hide" class="hide btn" id="hide">Show spoiler</a>
    <a href="#show" class="show btn" id="show">Hide spoiler</a>
    <div class="spoiler">
      <p class="spoiler-content">People die when they are killed!</p>
    </div>
</div>

CSS

.spoiler {
  display: none;
}

.show {
  display: none; 
}

.hide:target + .show {
  display: inline; 
}

.hide:target {
  display: none; 
}

.hide:target ~ .spoiler {
  display: inline;
}

/* Just for prettiness, not actually needed */

body {
  margin: 0;
  padding: 20px;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 20px;
  color: #333333;
  background-color: #ffffff;
}

.btn {
  padding: 4px 12px;
  margin-bottom: 0;
  *margin-left: .3em;
  font-size: 14px;
  line-height: 20px;
  color: #333333;
  text-align: center;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
  vertical-align: middle;
  cursor: pointer;
  background-color: #f5f5f5;
  *background-color: #e6e6e6;
  background-image: -moz-linear-gradient(top, #ffffff, #e6e6e6);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#ffffff), to(#e6e6e6));
  background-image: -webkit-linear-gradient(top, #ffffff, #e6e6e6);
  background-image: -o-linear-gradient(top, #ffffff, #e6e6e6);
  background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
  background-repeat: repeat-x;
  border: 1px solid #bbbbbb;
  *border: 0;
  border-color: #e6e6e6 #e6e6e6 #bfbfbf;
  border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
  border-bottom-color: #a2a2a2;
  -webkit-border-radius: 4px;
     -moz-border-radius: 4px;
          border-radius: 4px;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffffff', endColorstr='#ffe6e6e6', GradientType=0);
  filter: progid:DXImageTransform.Microsoft.gradient(enabled=false);
  *zoom: 1;
  -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
     -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
          box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  text-decoration: none;
}

.forum-post {
  padding: 20px;
  border: 1px solid #000;
}

.spoiler-content {
  padding: 15px;
}

.spoiler, .spoiler2{ 
  color: black; 
  background-color:black;
}

.spoiler:hover{
  color: white;
  }

.spoiler2:hover { 
  background-color:white; 
  }
<span class="spoiler" >test</span>

<p>Then when hovered over</p>

<span class="spoiler2"> other test </span>

Tags:

Html

Css