you're reading:
CSS editing, Hover effects, Html & inline css codes, Mouseover effects, Rollover effects, Spoiler text

Spoiler/hover content

http://wpbtips.wordpress.com/

Users sometimes ask how to create content that shows up only upon hover. If you have the Custom Design upgrade, this is rather easy. If you don’t have the upgrade, it’s very complicated but possible.

A. If you have the Custom Design upgrade

The solution I’m suggesting here makes the post column expand vertically to reveal the spoiler content when you hover over a word such as “Spoiler” – or any other word or phrase you might prefer, as long as you’d like to mark the existence of each and every spoiler with the same word or phrase.

(If you are interested in a different solution, you can post your question in the wordpress.com CSS forum.)

Add this code in the CSS editor:

.spoiler {
margin-bottom: 1em;
}
.spoiler:before {
content: "Spoiler";
text-decoration: underline;
}
.spoiler p {
display: none;
}
.spoiler:hover {
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
margin-top: -4px;
padding-top: 3px;
padding-bottom: 1em;
}
.spoiler:hover p {
display: block;
margin: 1em 0 0;
}

And format each spoiler this way in the so-called Text editor (make sure there’s a blank line between the opening div and the beginning of the text):

<div class="spoiler">

SPOILER TEXT HERE
(AND/OR IMAGE CODE)

</div>

Notice that the word “Spoiler” (or the word or phrase you’ll use instead of this one in the content rule) is incorporated in the CSS, so it’s introduced automatically: you don’t actually type it in the Text editor before each spoiler.

The suggested CSS is a discreet version, with the word “Spoiler” underlined, and no differentiation in the spoiler text (other than a top and a bottom border to separate it from the regular content). Naturally you can add more CSS rules to change the styling.

I don’t have the CSS upgrade on this blog, so I can’t display an example here. Here’s a screenshot (for a live example see this demo post):
spoiler text example

B. If you don’t have the Custom Design upgrade

A simple way to create rollover text would be this code in the Text editor:

<abbr title="ROLLOVER TEXT HERE">UNDERLINED WORD OR PHRASE HERE</abbr>

For the result, hover over this word. It’s a crude solution, as you cannot change the styling of the rollover text or add line or paragraph breaks.

Other than this, WP staff would probably say you can’t create hover effects without the upgrade. This isn’t exactly true, because our blogs include rollover items that are produced solely by means of CSS: namely, the dropdown or flyout menu lists. So you can create similar effects if you use the IDs and classes that are used for these lists.

The basic coding would be something like this:

<div id="access">
<ul class="menu"><li class="menu-item">Spoiler
<ul class="sub-menu">
<li class="menu-item">SPOILER TEXT HERE</li>
</ul>
</li>
</ul>
</div>

Unfortunately a) the parts I have highlighted vary from theme to theme, b) in most cases the default styling will be monstrous in a post (since it’s designed for the top menu), so you’ll need lots of additional coding to modify it. To find what the appropriate IDs and classes are, as well as what properties and values you should change, you need to examine the stylesheet of the theme you’re using (best way: the Firebug add-on in Firefox). So I’m only pointing this out as a general possibility – the rest depends on your coding skills.

Here’s an example (hover over the red text):

And here’s another one:

http://wpbtips.wordpress.com/

Please don’t paste code in comments – see comment guidelines.
About these ads

Discussion

18 thoughts on “Spoiler/hover content

  1. I’ve been wanting to do something like that last example (the “her” hover) for a long time panos, say for before and after image effects. I love to hear any suggestions you might have!

    Posted by IzaakMak | March 16, 2013, 23:01
  2. Amazing, Panos! :-)

    Posted by Galois | March 16, 2013, 23:02
  3. Very elegant! Congratulations. The photo especially–it’s almost a magical the way the image appears and then disappears when the cursor passes over the text!
    I’ve been wanting a clunkier version of this, where a word or phrase is a link that the reader can choose to click on, and have, say, a definition or an image come up. Is that a possibility? (I don’t have the css upgrade, and probably can’t do anything as complex as what you outlined above, so haven’t looked very hard into this). That simple rollover text would probably be fine for a short definition, come to think of it…Thanks, Panos!

    Posted by ingridcc | March 17, 2013, 05:22
  4. @GALOIS & INGRIDCC:
    Thanks! As you probably know, I like inventing workarounds…

    @IZAAKMAK:
    But the trick you’re asking for might take hours to perfect, especially on 2010. If I needed it for my blog I might do it, otherwise no: you’d have to pay me a lot more than the Custom Design upgrade would cost you! (If you have the upgrade, you can do it in a couple of minutes. In fact, I’ll publish a post on how to do this if you have the upgrade.) To display begore/after image pairs without the upgrade, you could use a slideshow.

    @INGRIDCC:
    Clicking instead of hovering to reveal some hidden content is a different thing: it would require javascript, which we’re not allowed to use. To reveal an image, the best you can do with the available means (and without far-fetched solutions like the one I’m demonstrating on this post) is create a regular link that links to an image file in a new browser window.

    Posted by Panos | March 17, 2013, 12:14
  5. I just found this. Brilliant!

    Posted by timethief | March 21, 2013, 04:39
  6. Thanks! Brilliant maybe, but of little use to the average user who cannot work out the required coding…

    Posted by Panos | March 21, 2013, 06:44
  7. Oh Panos….

    Posted by Duto Sri Cahyono | April 5, 2013, 21:43
  8. Hi! This is exactly what I want to do. But it doesn’t work. The hidden text (to be shown with the spoiler) never hides. Only the line appears when the cursor moves over the spoiler. Can you help please? Here is a link to code, CSS and result print screens. Thanks in advance!
    http://www.flickr.com/photos/95854568@N04/8752494517/in/photostream/

    Posted by dominadeau | May 19, 2013, 11:28
  9. It’s not working because you omitted the blank line between the div and the spoiler text. My version targets paragraphs, and the WP editor doesn’t create a correctly formed paragraph if you start it immediately after an opening div tag.

    And of course you can’t call this a spoiler in a CV: the word in the content rule should be changed to “Details” or something like that.

    By the way, I don’t need screenshots: as long as I have a link to your site, I can see your code (as well as copy it and reproduce it in my tests blog, if necessary).

    Posted by Panos | May 19, 2013, 15:24
  10. I’ve just found another way to create spoiler. It is using the details tag. But unfortunately it is currently only supported in Chrome and in Safari 6. Hope Firefox will support it soon.

    Posted by ~ Yappa ~ | May 21, 2013, 06:38
  11. Thanks. That’s a new tag, and of course when it comes to spoilers we can’t suggest a way that doesn’t work in all browsers.

    Posted by Panos | May 21, 2013, 14:54
  12. Thanks a lot Panos! Now working, but not on iPad and iPhone. Any solution?
    http://dominiquegnadeau.wordpress.com/curriculum-vitae/

    Posted by dominadeau | May 22, 2013, 06:33
  13. You’re welcome.
    Unfortunately I don’t have experience with iPads and such, and I don’t have one at hand at the moment (my girlfriend forgot hers at another house!), so I can’t check things to see what might be causing this. We’ll probably have the iPad back by tomorrow or day after tomorrow, but you’d better ask about this in the wordpress.com CSS forum, where you may get a reply from WP staff.

    Posted by Panos | May 23, 2013, 04:39
  14. oh!! nice.. is this available in wordpress plugin?? if yes.. link plzz.. :)

    Posted by Muzammil Syed S | July 26, 2013, 14:06
  15. Can you take a look in koi theme and suggest me what to do with the last part in your post?

    Posted by thai92hp | July 28, 2013, 00:24
  16. http://thai92hp.wordpress.com/
    Help me with this blog.

    Posted by thai92hp | July 28, 2013, 08:43

Trackbacks/Pingbacks

  1. Pingback: Spoiler Schaltfläche mit CSS3 | WPcom Experimente - October 23, 2013

Author

author's avatar panos (justpi)

 Subject Index

Announcement 22/03/2012: After WP's latest move, this blog will no longer offer active support and assistance. The blog will remain online but commenting on older posts has been disabled.
✶ All theme-related posts are updated up to and including theme 189 in this list, but will not continue to be updated.

Stats

  • 2,260,548 views
  • Views per month:
Safari Icon Firefox - Never Internet Explorer
Note: if you see ads on this site, they are placed by WordPress, not me.
wpbtips.wordpress.com
Mostly on themes, formatting, coding, tweaks and workarounds.
Based on or springing from my contributing in the wp.com forum.
Theme-related posts constantly updated
Premium themes and Annotum not included
%d bloggers like this: