WordPress Themes Blog Oh! Blog

Share on Facebook0Tweet about this on TwitterShare on Google+10Pin on Pinterest2Share on LinkedIn8

CSS3 has really empowered the web developer to add beauty, interactivity and animation to his web pages. The dependency on JavaScript for simple effects has reduced to a great level with the arrival of CSS3.

Just to showcase the power of CSS3, lets learn how to make a Polaroid style image gallery using CSS3 and HTML5.


First, make a new HTML file and copy the following code into the file :-

<!doctype html>
<meta charset="utf-8">
<title>CSS3/HTML5 Polaroid Gallery</title>
<link type="text/css" href="gallerystyle.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Nothing+You+Could+Do' rel='stylesheet' type='text/css'>

<h1>CSS3 - HTML5 Polaroid Gallery</h1>
<ul class="pics">
<li><a href="" title="Sports"><img src="http://lorempixel.com/300/300/sports" /></a></li>
<li><a href="" title="Random"><img src="http://lorempixel.com/200/200" /></a></li>
<li><a href="" title="Animal"><img src="http://lorempixel.com/320/320/animals" /></a></li>
<li><a href="" title="Food"><img src="http://lorempixel.com/340/340/food" /></a></li>
<li><a href="" title="City"><img src="http://lorempixel.com/400/400/city" /></a></li>
<li><a href="" title="Transport"><img src="http://lorempixel.com/320/320/transport" /></a></li>
<li><a href="" title="Fashion"><img src="http://lorempixel.com/400/400/fashion" /></a></li>
</ul><!--pics -->

The referenced stylesheet file is called gallerystyle.css which we will build later. I have also included a Google Font called “Nothing You Could Do” which is a cursive/handwriting font.
The HTML code in the body is creating a list of images that are being pulled randomly from Lorempixel.com. Just so that you know, this website allows you to create dummy images of different category and call them in your webpage for prototyping purposes (similar to lorem ipsum text). You can even define the size of the images that you want.


Next, make a new stylesheet file and name it gallerystyle.css. Save it in the same location where your HTML file is located. Copy the following code into it :-

* {margin:0;padding:0;list-style:none;}
body {background:url(bg.png);}
h1 {text-align:center;font-family:'Nothing You Could Do', cursive;padding:10px;text-shadow:1px 1px #333;background-color:#000;color:#fff;}
.pics {margin:auto;margin:0px auto;position:relative;height:500px;width:800px;}
.pics a {background:#fff;display:inline;float:left;width:auto;padding:10px 10px 30px 10px;text-align:center;text-decoration:none;color:#333;font-size:28px;font-family:'Nothing You Could Do', cursive;font-weight:bold;-webkit-box-shadow:0px 0px 10px rgba(0,0,0,1);-moz-box-shadow:0px 0px 10px rgba(0,0,0,1);box-shadow:0px 0px 10px rgba(0,0,0,1);-webkit-transition:all .30s;-moz-transition:all .30s;-o-transition:all .30s;transition:all .30s;position:absolute;}
.pics img {display:block;padding-bottom:20px;}
.pics a:hover {z-index:100;}
.pics a:after {content:attr(title);}

In the above CSS code, we have made a container called .pics which will wrap all the images. The images are called in list items which are again wrapped in anchor links. We are applying general styling through .pics a to make our images look cool like Polaroid snaps by adding padding, border and drop shadow. We also add transition time which will help us animate the images when hovered.

So that we don’t have to write extra code for image captions, we are using the title attributes of the anchor links and making them show as content through CSS3.

Next add the following code to the stylesheet file again :-

/* Pictures placement, scaling and rotation */
.pics li:nth-child(1n) a {-webkit-transform:rotate(30deg) scale(0.7,0.7);-moz-transform:rotate(30deg) scale(0.7,0.7);transform:rotate(30deg) scale(0.7,0.7);top:0%;left:60%;}
.pics li:nth-child(2n) a {-webkit-transform:rotate(-30deg) scale(0.7,0.7);-moz-transform:rotate(-30deg) scale(0.7,0.7);transform:rotate(-30deg) scale(0.7,0.7);top:40%;left:80%;}
.pics li:nth-child(3n) a {-webkit-transform:rotate(40deg) scale(0.7,0.7);-moz-transform:rotate(40deg) scale(0.7,0.7);transform:rotate(40deg) scale(0.7,0.7);top:50%;left:-10%;}
.pics li:nth-child(4n) a {-webkit-transform:rotate(-30deg) scale(0.7,0.7);-moz-transform:rotate(-30deg) scale(0.7,0.7);transform:rotate(-30deg) scale(0.7,0.7);top:0%;left:-10%;}
.pics li:nth-child(5n) a {-webkit-transform:rotate(-20deg) scale(0.8,0.8);-moz-transform:rotate(-20deg) scale(0.8,0.8);transform:rotate(-20deg) scale(0.8,0.8);top:50%;left:20%;z-index:3;}
.pics li:nth-child(6n) a {-webkit-transform:rotate(30deg) scale(0.7,0.7);-moz-transform:rotate(30deg) scale(0.7,0.7);transform:rotate(30deg) scale(0.7,0.7);top:60%;left:55%;}
.pics li:nth-child(7n) a {-webkit-transform:rotate(10deg) scale(0.7,0.7);-moz-transform:rotate(10deg) scale(0.7,0.7);transform:rotate(10deg) scale(0.7,0.7);top:0%;left:25%;}

We are using the nth-child property to control the rotation, scaling and placement of the images. This will sequentially find the list items and apply the styling to each one of them. This has been done like this, so that we don’t have to make classes for each image and style them individually. Now, you just have to change the image paths in the HTML code and the styling will be applied automatically. For now, there are 7 images. You can add new images and make new style by increasing the nth-child(8n…9n…10n) count. To make any image on top, the z-index for that image has to be increased. The higher the z-index will be, that image will be on top of any other with lower z-index.

Finally, add the following code to animate the images on hover. This will rotate and scale the images to their original sizes.

/* Pictures rotation and scaling on hover */
.pics li:nth-child(n) a:hover {-webkit-transform:rotate(0deg) scale(1,1);-moz-transform:rotate(0deg) scale(1,1);transform:rotate(0deg) scale(1,1);}

You can see the working demo and download the files below :-
CSS3 Polaroid Gallery Demo
Download Files

What Next?

Leave a comment if you like this CSS3 Polaroid Gallery and if I get good number of requests, I can turn this into a FREE WordPress Theme for you people. 🙂

Share on Facebook0Tweet about this on TwitterShare on Google+10Pin on Pinterest2Share on LinkedIn8
  1. ??????? ???????? left a comment on May 16, 2016 at 2:31 pm |

    thanks for these codes , very goood

  2. huagong left a comment on March 21, 2016 at 9:38 pm |

    HGIT is the professional manufacturer for two axis foam sealing machine, this machine can be used in Filter, cabinet, lighting, battery, etc.
    German Beckhoff dispensing operation system. Operation interface is indicated in both Chinese and English; if there is any need, other languages can be added. The system boasts functions such as false operation reminding, idiot-proof protection, overpressure alarming, temperature alarming and low material level reminding.
    When operating the human-machine interface, the operator only needs to enter commands in the interface such as “Confirm” and “Start” for loading and unloading of workpieces, and then the equipment can automatically complete the required function operations.
    The control system is CNC2013 control system independently researched and developed by the Company. It can control multiple axes simultaneously and control dispensing speed and glue flow, with multi-formula storage function, pressure alarming function, etc. which makes it safer and user-friendly.
    The dispensing valve and mixer match with each other well, easy to operate and convenient for maintenance.
    Two components metering pump can choose Domestic pump or Germany pump according to customer demand

  3. Rita B. clark left a comment on July 25, 2015 at 11:25 am |

    Thanks for sharing the code on polaroid gallery.

  4. Dalvir Kaur left a comment on June 6, 2015 at 2:05 am |

    In really like ur post frnd…it is amazing…m also web designer nd blogger.

  5. Bhumi left a comment on February 18, 2015 at 7:14 am |

    Thank you for the great post.

  6. Objects left a comment on January 16, 2015 at 6:13 am |

    for web designer like me its useful , Now checking working demo then will implement on website 🙂

  7. Imtiaj left a comment on January 9, 2015 at 4:12 am |

    In order to add photos to your gallery, you need to edit them to fit your need. Movavi is one of the best photo editor that is very easy to use. See my review of Movavi Photo Editor here – http://www.pcappspot.com/movavi-photo-editor-review/

  8. free wordpress themes left a comment on December 22, 2014 at 1:46 pm |

    Thanks for sharing the code on polaroid gallery.

  9. mersin evden eve left a comment on November 11, 2014 at 8:46 am |

    harika bilgiler thank you

  10. gikarasojo left a comment on August 21, 2014 at 8:29 am |

    thanks for this css trick. its awesome. however, i have a question for you. does the value of 1st n continue to increment event for the lest n elements? e.g
    if we say li:nth-child(1n) what i know is that the value of n here is zero. now if i write onther list item behind this e.g li:nth-child(2n) will the value of n be zero or will it increment to one? thanks in advance


Leave a Comment

Your email address will not be published. Required fields are marked *

What Others Say

BannerJai was awesome to work with. A port of an ASPX/HTML site to Wordpress went seamlessly, and his recreation of our look and feel were fantastic - Kevin Webster

BannerJai is a talented, creative and an enthusiastic web developer who has worked consistently to deliver high quality websites for my businesses - James Eccles

  •   Let’s Talk

    "Thank you for visiting my website! In order to start a project with me, please send me information on your project so I can figure out how best to help you."

    "After your initial contact, I’ll schedule a quick one-on-one session via Skype to review the project and get to know you."
     Request a Quote
  Have Questions?   Hire Me

Send Me Your Questions

Tell Me About Your Project