Jon
Rohan

I'm a front end web developer living in San Francisco. I have been coding HTML since I was 15. I strive to be the best I can at what I do. I believe in clean and simple design as well as code. This is where I represent myself on the web. I am always looking to form relationships with other amazing people, part time or full time. contact me!

Using CSS3′s :target pseudo-class to Make a Slideshow

CSS3 has a lot of cool pseudo-classes. For this experiment I used the :target pseudo-class which lets you target css element based on the element targeted at the end of a url, for example.

http://example.com/page#article1

The :target on this page would be the element with the id="article1". So if you wanted to place a red dotted border around article1, then you would use something like this.

:target { 
  border: 1px dotted #FF0000; 
}

So I decided to try an experiment and make a slideshow using nothing but HTML and CSS.

Demo

Sorry, but no IE’s allowed. You prob won’t see anything.

Start Slideshow

Code

<ul class="book-pages">
  <li id="1">
    <div>
      <h3>Taming CSS</h3>
      <small>by Jon Rohan</small>
    </div>
    <span class="slide-info">Slide 1 of 3</span>
    <a href="#2" class="next">Next</a>
  </li>
  <li id="2">
    <div>
      <h3>Never Assume you've mastered it</h3>
    </div>
    <span class="slide-info">Slide 2 of 3</span>
    <a href="#1" class="last">Last</a>
    <a href="#3" class="next">Next</a>
  </li>
  <li id="3">
    <div>
      <h3>The End</h3>
    </div>
    <span class="slide-info">Slide 3 of 3</span>
    <a href="#2" class="last">Last</a>
  </li>
</ul>

The HTML is a basic unordered list. Each list item will be the slide. Inside each slide is the controls to navigate backward and forward. And a span to display the current slide number.

The main CSS that controls the slideshow, is to just display the current slide, like this.

.book-pages li:target {
  display:block;
}

And here’s the rest of the CSS.

.book-pages, .book-pages li {
  list-style-type:none;
  margin:0;
  padding:0;
}
ul.book-pages {
  margin:30px 0;
}
.book-pages li {
  background-color:#111111;
  display:none;
  height:350px;
  margin:0 auto;
  position:relative;
  text-align:center;
  width:510px;
  color: #ffffff;
}
.book-pages div {
  font-size:1.8em;
  padding:120px 25px 0;
  line-height:2em;
}
.book-pages li:target {
  display:block;
}
.book-pages .next {
  position:absolute;
  right:0;
  bottom:-20px;
}
.book-pages .last {
  position:absolute;
  left:0;
  bottom:-20px;
}
.book-pages .slide-info {
  position:absolute;
  bottom:-20px;
  left:44%;
  color:#333333;
}

If you enjoyed reading this post, check out these posts CSS 3 @font-face, CSS Text Rotation Clock, Submit to Digg Bookmarklet.

Tags: , , , , , 28 Sep 2009

About the Author, Jon Rohan

Hey! Thanks for stopping by. I love posting new topics here about various development things. If you liked it follow me on twitter and let me know.

Subscribe to Posts

2 comments

Add comment