CSS Photo Gallery

We can use lists and CSS to create an animated photo gallery.

Basic Photo Gallery Layout

The following example displays a basic photo gallery without any effects.

Example of laying out of images in a list (Run Example)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body
{
  background-color:#eee; 
}

div.photogallery ul li
{
  display:inline-block;  /* display list items beside each other */
  background-color:white;
  width:250px;   /* width of each image */
  text-align:center;  /* text under image */
  padding:10px; 
  margin:40px;
}

div.photogallery ul li:hover 
{
  position: relative;
  z-index: 5; /* place this image on the top */ 
}

div.photogallery ul li img
{
  width:100%; /* img takes up full width of containing li */
  padding-bottom:10px;  /* push text down a bit */
}
</style>
</head>

<body>

<div class="photogallery">
<ul>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>        
</ul>
</div>
</body>
</html>

Photo Gallery with Drop Shadow

The following example displays a photo gallery with drop shadows.

Example of photo gallery with drop shadow effect (Run Example)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body
{
  background-color:#eee; 
}

div.photogallery ul li
{
  display:inline-block;  /* display list items beside each other */
  background-color:white;
  width:250px;   /* width of each image */
  text-align:center;  /* text under image */
  padding:10px; 
  margin:40px;
}

div.photogallery ul li:hover 
{
  position: relative;
  z-index: 5; /* place this image on the top */ 
}

div.photogallery ul li img
{
  width:100%; /* img takes up full width of containing li */
  padding-bottom:10px;  /* push text down a bit */
}


/* Additional styles to give the images a drop shadow */
div.photogallery ul li
{
  -webkit-box-shadow: 0 3px 6px rgba(5,5,50,.5); /* drop shadow */
  -moz-box-shadow: 0 3px 6px rgba(5,5,50,.5);
  -ms-box-shadow: 0 3px 6px rgba(5,5,50,.5);  
  -o-box-shadow: 0 3px 6px rgba(5,5,50,.5);    
  box-shadow: 0 3px 6px rgba(5,5,50,.5);
}

div.photogallery ul li:hover 
{   
  -webkit-box-shadow: 0 3px 6px rgba(35,35,0,.8);  /* darken drop shadows */
  -moz-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  -ms-box-shadow: 0 3px 6px rgba(35,35,0,.8);  
  -o-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  box-shadow: 0 3px 6px rgba(35,35,0,.8);    
}
</style>
</head>

<body>

<div class="photogallery">
<ul>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>        
</ul>
</div>
</body>
</html>

Photo Gallery with Scaling

The following example displays a photo gallery that scales when the mouse hovers over an image.

Example of photo gallery with scaling (Run Example)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body
{
  background-color:#eee; 
}

div.photogallery ul li
{
  display:inline-block;  /* display list items beside each other */
  background-color:white;
  width:250px;   /* width of each image */
  text-align:center;  /* text under image */
  padding:10px; 
  margin:40px;
}

div.photogallery ul li:hover 
{
  position: relative;
  z-index: 5; /* place this image on the top */ 
}

div.photogallery ul li img
{
  width:100%; /* img takes up full width of containing li */
  padding-bottom:10px;  /* push text down a bit */
}


/* Additional styles to give the images a drop shadow */
div.photogallery ul li
{
  -webkit-box-shadow: 0 3px 6px rgba(5,5,50,.5); /* drop shadow */
  -moz-box-shadow: 0 3px 6px rgba(5,5,50,.5);
  -ms-box-shadow: 0 3px 6px rgba(5,5,50,.5);  
  -o-box-shadow: 0 3px 6px rgba(5,5,50,.5);    
  box-shadow: 0 3px 6px rgba(5,5,50,.5);
}

div.photogallery ul li:hover 
{   
  -webkit-box-shadow: 0 3px 6px rgba(35,35,0,.8);  /* darken drop shadows */
  -moz-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  -ms-box-shadow: 0 3px 6px rgba(35,35,0,.8);  
  -o-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  box-shadow: 0 3px 6px rgba(35,35,0,.8);    
}


/* Additional style to scale the hovered image */
div.photogallery ul li:hover 
{   
  -webkit-transform: scale(1.25);  /* scale */
  -moz-transform: scale(1.25);
  -ms-transform: scale(1.25);  
  -o-transform: scale(1.25);
  transform: scale(1.25); 
}
</style>
</head>

<body>

<div class="photogallery">
<ul>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>        
</ul>
</div>
</body>
</html>

Photo Gallery with Transformations

The following example displays a photo gallery with various transformations.

Example of photo gallery with transformations (Run Example)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body
{
  background-color:#eee; 
}

div.photogallery ul li
{
  display:inline-block;  /* display list items beside each other */
  background-color:white;
  width:250px;   /* width of each image */
  text-align:center;  /* text under image */
  padding:10px; 
  margin:40px;
}

div.photogallery ul li:hover 
{
  position: relative;
  z-index: 5; /* place this image on the top */ 
}

div.photogallery ul li img
{
  width:100%; /* img takes up full width of containing li */
  padding-bottom:10px;  /* push text down a bit */
}


/* Additional styles to give the images a drop shadow */
div.photogallery ul li
{
  -webkit-box-shadow: 0 3px 6px rgba(5,5,50,.5); /* drop shadow */
  -moz-box-shadow: 0 3px 6px rgba(5,5,50,.5);
  -ms-box-shadow: 0 3px 6px rgba(5,5,50,.5);  
  -o-box-shadow: 0 3px 6px rgba(5,5,50,.5);    
  box-shadow: 0 3px 6px rgba(5,5,50,.5);
}

div.photogallery ul li:hover 
{   
  -webkit-box-shadow: 0 3px 6px rgba(35,35,0,.8);  /* darken drop shadows */
  -moz-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  -ms-box-shadow: 0 3px 6px rgba(35,35,0,.8);  
  -o-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  box-shadow: 0 3px 6px rgba(35,35,0,.8);    
}


/* Additional styles to place images in a pseudo-random layout */
div.photogallery ul li:nth-of-type(odd) 
{
  -webkit-transform: rotate(2deg);
  -moz-transform: rotate(2deg);
  -ms-transform: rotate(2deg);
  -o-transform: rotate(2deg);  
  transform: rotate(2deg);  
}
 
div.photogallery ul li:nth-of-type(even) 
{
  -webkit-transform: rotate(-3deg);
  -moz-transform: rotate(-3deg);
  -ms-transform: rotate(-3deg);
  -o-transform: rotate(-3deg);
  transform: rotate(-3deg);  
}

div.photogallery ul li:nth-of-type(5) 
{
  -webkit-transform: rotate(1deg);
  -moz-transform: rotate(1deg);
  -ms-transform: rotate(1deg);
  -o-transform: rotate(1deg);
  transform: rotate(1deg);  
}
 
div.photogallery ul li:nth-of-type(10)  
{
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -ms-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);  
}
 
div.photogallery ul li:nth-of-type(3)  
{
  -webkit-transform: translate(3px, -2px);
  -moz-transform: translate(-3px, -2px);
  -ms-transform: translate(3px, -2px);
  -o-transform: translate(-3px, -2px);
  transform: translate(-3px, -2px);
}

div.photogallery ul li:nth-of-type(7)  
{
  -webkit-transform: translate(10px, -15px);
  -moz-transform: translate(10px, -15px);
  -ms-transform: translate(10px, -15px);
  -o-transform: translate(10px, -15px);
  transform: translate(10px, -15px); 
}

div.photogallery ul li:nth-of-type(8)  
{
  -webkit-transform: translate(-15px, 10px);
  -moz-transform: translate(-15px, 10px);
  -ms-transform: translate(-15px, 10px);
  -o-transform: translate(-15px, 10px);
  transform: translate(-15px, 10px);
}


/* Additional style to scale the hovered image */
div.photogallery ul li:hover 
{   
  -webkit-transform: scale(1.25);  /* scale */
  -moz-transform: scale(1.25);
  -ms-transform: scale(1.25);  
  -o-transform: scale(1.25);
  transform: scale(1.25); 
  
}


</style>
</head>

<body>

<div class="photogallery">
<ul>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>        
</ul>
</div>
</body>
</html>

Photo Gallery with Transitions

The following example displays a photo gallery with a transition for the hover effect.

Example of photo gallery with a hover transition (Run Example)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body
{
  background-color:#eee; 
}

div.photogallery ul li
{
  display:inline-block;  /* display list items beside each other */
  background-color:white;
  width:250px;   /* width of each image */
  text-align:center;  /* text under image */
  padding:10px; 
  margin:40px;
}

div.photogallery ul li:hover 
{
  position: relative;
  z-index: 5; /* place this image on the top */ 
}

div.photogallery ul li img
{
  width:100%; /* img takes up full width of containing li */
  padding-bottom:10px;  /* push text down a bit */
}


/* Additional styles to give the images a drop shadow */
div.photogallery ul li
{
  -webkit-box-shadow: 0 3px 6px rgba(5,5,50,.5); /* drop shadow */
  -moz-box-shadow: 0 3px 6px rgba(5,5,50,.5);
  -ms-box-shadow: 0 3px 6px rgba(5,5,50,.5);  
  -o-box-shadow: 0 3px 6px rgba(5,5,50,.5);    
  box-shadow: 0 3px 6px rgba(5,5,50,.5);
}

div.photogallery ul li:hover 
{   
  -webkit-box-shadow: 0 3px 6px rgba(35,35,0,.8);  /* darken drop shadows */
  -moz-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  -ms-box-shadow: 0 3px 6px rgba(35,35,0,.8);  
  -o-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  box-shadow: 0 3px 6px rgba(35,35,0,.8);    
}


/* Additional styles to place images in a pseudo-random layout */
div.photogallery ul li:nth-of-type(odd) 
{
  -webkit-transform: rotate(2deg);
  -moz-transform: rotate(2deg);
  -ms-transform: rotate(2deg);
  -o-transform: rotate(2deg);  
  transform: rotate(2deg);  
}
 
div.photogallery ul li:nth-of-type(even) 
{
  -webkit-transform: rotate(-3deg);
  -moz-transform: rotate(-3deg);
  -ms-transform: rotate(-3deg);
  -o-transform: rotate(-3deg);
  transform: rotate(-3deg);  
}

div.photogallery ul li:nth-of-type(5) 
{
  -webkit-transform: rotate(1deg);
  -moz-transform: rotate(1deg);
  -ms-transform: rotate(1deg);
  -o-transform: rotate(1deg);
  transform: rotate(1deg);  
}
 
div.photogallery ul li:nth-of-type(10)  
{
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -ms-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);  
}
 
div.photogallery ul li:nth-of-type(3)  
{
  -webkit-transform: translate(3px, -2px);
  -moz-transform: translate(-3px, -2px);
  -ms-transform: translate(3px, -2px);
  -o-transform: translate(-3px, -2px);
  transform: translate(-3px, -2px);
}

div.photogallery ul li:nth-of-type(7)  
{
  -webkit-transform: translate(10px, -15px);
  -moz-transform: translate(10px, -15px);
  -ms-transform: translate(10px, -15px);
  -o-transform: translate(10px, -15px);
  transform: translate(10px, -15px); 
}

div.photogallery ul li:nth-of-type(8)  
{
  -webkit-transform: translate(-15px, 10px);
  -moz-transform: translate(-15px, 10px);
  -ms-transform: translate(-15px, 10px);
  -o-transform: translate(-15px, 10px);
  transform: translate(-15px, 10px);
}


/* Additional style to transition when hovering */
div.photogallery ul li
{
   -moz-transition:0.3s; 
   -webkit-transition: 0.3s;
   -ms-transition: 0.3s;
   -o-transition: 0.3s;
   transition: 0.3s;
}
</style>
</head>

<body>

<div class="photogallery">
<ul>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>        
</ul>
</div>
</body>
</html>

Scattered Photo Gallery

To make a scattered photo gallery, replace the line display:inline-block; from the div.photogallery ul li style with position:absolute; This will cause all of the images to display on top of each other. A side effect of this is that the bullets for the list items will return. Insert the code display:block; to remove the bullets. By adjusting the various translations, the images can be scattered over the whole screen. Removing the scaling from the hover will cause the hovered image to display at the front of the photo stack. As the selected is simply displayed at the top of the stack, there is no longer any need to have the transition style. An example of a stack of scattered images is shown below.

Example of stack of scattered images (Run Example)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body
{
  background-color:#eee; 
}

div.photogallery ul li
{
  display:block;      /* remove the bullet from the list item */
  position:absolute;  /* allow photos to be displayed anywhere on the screen */
  background-color:white;
  width:250px;   /* width of each image */
  text-align:center;  /* text under image */
  padding:10px; 
  margin:40px;
}

div.photogallery ul li:hover 
{
  z-index: 5; /* place this image on the top */ 
}

div.photogallery ul li img
{
  width:100%; /* img takes up full width of containing li */
  padding-bottom:10px;  /* push text down a bit */
}



/* Additional styles to give the images a drop shadow */
div.photogallery ul li
{
  -webkit-box-shadow: 0 3px 6px rgba(5,5,50,.5); /* drop shadow */
  -moz-box-shadow: 0 3px 6px rgba(5,5,50,.5);
  -ms-box-shadow: 0 3px 6px rgba(5,5,50,.5);  
  -o-box-shadow: 0 3px 6px rgba(5,5,50,.5);    
  box-shadow: 0 3px 6px rgba(5,5,50,.5);
}

div.photogallery ul li:hover 
{   
  -webkit-box-shadow: 0 3px 6px rgba(35,35,0,.8);  /* darken drop shadows */
  -moz-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  -ms-box-shadow: 0 3px 6px rgba(35,35,0,.8);  
  -o-box-shadow: 0 3px 6px rgba(35,35,0,.8);
  box-shadow: 0 3px 6px rgba(35,35,0,.8);    
}


/* Additional styles to place images in a pseudo-random layout */
div.photogallery ul li:nth-of-type(odd) 
{
  -webkit-transform: rotate(20deg) translate(200px, 150px);
  -moz-transform: rotate(20deg) translate(200px, 150px);
  -ms-transform: rotate(20deg) translate(200px, 150px);
  -o-transform: rotate(20deg) translate(200px, 150px);  
  transform: rotate(20deg) translate(200px, 150px);  
}
 
div.photogallery ul li:nth-of-type(even) 
{
  -webkit-transform: rotate(-30deg) translate(100px, 350px);
  -moz-transform: rotate(-30deg) translate(100px, 350px);
  -ms-transform: rotate(-30deg) translate(100px, 350px);
  -o-transform: rotate(-30deg) translate(100px, 350px);
  transform: rotate(-30deg) translate(100px, 350px);  
}

div.photogallery ul li:nth-of-type(3) 
{
  -webkit-transform:translate(80px, 200px);
  -moz-transform: translate(80px, 200px);
  -ms-transform: translate(80px, 200px);
  -o-transform: translate(80px, 200px);
  transform: translate(80px, 200px); 
}
 
div.photogallery ul li:nth-of-type(4)  
{
  -webkit-transform: rotate(60deg);
  -moz-transform: rotate(60deg);
  -ms-transform: rotate(60deg);
  -o-transform: rotate(60deg);
  transform: rotate(60deg);  
}
 
div.photogallery ul li:nth-of-type(5)  
{
  -webkit-transform:  translate(500px, 400px);
  -moz-transform:  translate(500px, 400px);
  -ms-transform:  translate(500px, 400px);
  -o-transform: translate(500px, 400px);
  transform: translate(500px, 400px);  
}

div.photogallery ul li:nth-of-type(7) 
{
  -webkit-transform: rotate(100deg);
  -moz-transform: rotate(100deg) ;
  -ms-transform: rotate(100deg);
  -o-transform: rotate(100deg);
  transform: rotate(100deg);  
}
 
div.photogallery ul li:nth-of-type(9)  
{
  -webkit-transform: rotate(60deg) translate(400px, 150px);
  -moz-transform: rotate(60deg) translate(400px, 150px);
  -ms-transform: rotate(60deg) translate(400px, 150px);
  -o-transform: rotate(60deg) translate(400px, 150px);
  transform: rotate(60deg) translate(400px, 150px);  
}
 
div.photogallery ul li:nth-of-type(11)  
{
  -webkit-transform: rotate(-40deg) translate(500px, 400px);
  -moz-transform: rotate(-40deg) translate(500px, 400px);
  -ms-transform: rotate(-40deg) translate(500px, 400px);
  -o-transform: rotate(-40deg) translate(500px, 400px);
  transform: rotate(-40deg) translate(500px, 400px);  
}
</style>
</head>

<body>
<div class="photogallery">
<ul>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li> 
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>
   <li><a><img src="images/dkit01.gif"></a>First</li>
   <li><a ><img src="images/dkit02.gif"></a>Second</li>
   <li><a ><img src="images/dkit03.gif"></a>Third</li>
   <li><a ><img src="images/dkit04.gif"></a>Fourth</li> 
   <li><a><img src="images/logo_a.gif"></a>First</li>
   <li><a ><img src="images/logo_b.jpg"></a>Second</li>        
</ul>
</div>
</body>
</html>
 
<div align="center"><a href="../../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>