Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sam Coble
7,520 PointsPure CSS Toggle :active&:hover
I thought i'd share a useful trick with CSS that allows for toggle functionality without having to use a check box. I had a lot of trouble implementing the check box toggle method in all the posts on a site, especially if there are images that need to take up the screen temporarily and be closed easily without confusion.
- The check box toggle method is also known as the check box 'hack'
- Demo with photos: toggle demo
- CSS I used in a pastebin: code
A really quick rundown. :active allows for an element with :hover to be brought to the screen, keeping it self open with it's own :hover. To close it, :active is used to move it away from your mouse or stop the :hover allowing it to toggle off.
How it works in more detail
I use a transparent
<div>class named 'hook' that is always hidden with no width or height until I call for it, it's use is explained in the follow steps.This
<div>is given a:hoverproperty that makes itself stay open. (When it's opened, it stays open! It's a hook..)Using the CSS
:activeselector allows for the click to be captured and force our hidden child<div>to become enlarged over the entire screen withposition: fixed. Now that it's open it stays open on it's own.Now you've released the mouse and
:activeis no longer keeping our<div>in position, but we made it 'hook' on so it doesn't need a click anymore.The last step is to make the 'hook' unhook, simply add the
:activeselector to our 'hook' class that closes it self. I usedvisibility: hidden;andleft: -9999;to stop it from staying in place. This part can be changed with a any CSS that does the same job of stopping the hook.
The HTML I used
<div class = "gallery-post">
<div class = "hook"></div>
<img src="image.jpg">
<h1>Scarcely on striking</h1>
</div>
Placing the hook in all my posts allowed me to change the image directly after it with the selector ~
This is how I did it, i'm sure there are a few changes that could be made to my code but I think you get the idea.
1 Answer
Nicholas Olsen
Front End Web Development Techdegree Student 19,342 PointsWow, very cool! Thanks for sharing.
James Barnett
39,199 PointsJames Barnett
39,199 PointsInstead of posting both a demo and code to a pastebin, just use codepen.io, it combines both functions and it also updates in real time and makes it easy to fork/share.