Using :target to Create a Lightbox Effect with CSS3
If you’ve messed around with CSS3 you have probably seen some of the new pseudo classes pop up here and there. An interesting one that I just started to use is
:target. A full explanation of what it does can be found on the W3 site but for the purposes of this tutorial, we will be using it to create a Lightbox effect. I actually developed this for PressWork and so I decided to call it Pressbox.
First we need to create a thumbnail and surround it with an anchor tag.
<a href="#image1"><img src="http://yoursite.com/images/image1.jpg" alt="" /></a>
href parameter is very important since it will be what links our thumbnail to our larger image. We will use the same name as the ID in our larger image like so:
<a href="#" id="image1" class="pressbox"><img src="http://yoursite.com/images/image1-big.jpg" alt=""></a>
The larger image needs a class that we can reference. I used
pressbox but you can use whatever you want as long as you modify the CSS accordingly. Here is the CSS that will get everything working properly: