Using :target to Create a Lightbox Effect with CSS3
by c.bavota | Posted in Tutorials | 19 comments
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>
The 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:
.pressbox {
width: 0;
height: 0;
position: fixed;
overflow: hidden;
left: 0;
top: 0;
z-index: 9999;
text-align: center;
background: rgba(0,0,0,0.7);
}
.pressbox img {
opacity: 0;
padding: 10px;
background: #ffffff;
margin-top: 100px;
-webkit-box-shadow: 0px 0px 15px #444;
-moz-box-shadow: 0px 0px 15px #444;
box-shadow: 0px 0px 15px #444;
-moz-transition: opacity .25s ease-in-out;
-webkit-transition: opacity .25s ease-in-out;
transition: opacity .25s ease-in-out;
}
.pressbox:target {
width: auto;
height: auto;
bottom: 0;
right: 0;
}
.pressbox:target img {
opacity: 1;
}
We’ve added an opacity transition so that our Pressbox will fade in once we click on the thumbnail. This will all work because of the :target pseudo class that will display the the element that uses the same ID that is referenced by the href parameter in the thumbnail’s anchor tag.
You can test it out below.
Remember that this will only work in the following browsers:
- Firefox 1.0+
- Safari 3.1+
- Opera 9.5+
- Chrome 2+





heh, no kidding! That’s really cool. I just adapted that for using the WordPress featured image… I’m going to post my code up on VoodooPress, but link back to you as the source for the entire css/html/functionality, if you don’t mind!
Voodoo – I just had a read of your post… nice and simple and works like a charm. Nice one!
Matt
Great tip – thanks! Is there an easy way to make this backwards compatible with older browsers?
-Matt
Yeah, works. Testing to get it working with some older browsers, but got no success right now.
Greatest tip in this time i visit your blog !
Wow this is really cool. I found a similar one on designer depot but this one is great. Thanks for the tips.
Thank you this is beautifully simple! I just spent the last couple of hours trying to find a solution that doesn’t involve a plugin, then came across your blog. Works perfectly on my twentyeleven child theme. Is it possible to apply this to a default gallery within a post/page, i.e. having the ability to navigate between larger images?
Hi, great tip. Thanks
Thanks for the tip. This will save a lot time by cutting out the need for photoshop.
I love the fact that it is clean and quick. Good you mentioned Presswork .. got to check it out because it could save loads of time and make changes visible for customers much quicker.
Good work! Thanks for sharing this great tip! Thanks
Nice! Just one question… when I close the lightbox, it automatically sends me to the top of the page. I notice is does this on your page as well. How do I fix this?
Since it requires the use of adding a “#” number sign to the URL it will always direct to the top of the page since that is how all browsers react.
Good tip! Now to spend my night testing it out, lol.