Expandable Image Gallery using HTML, CSS & JavaScript

Expandable Image Gallery using HTML, CSS & JavaScript

Hello developers, today in this blog, you'll learn how to create an Expandable Image Gallery using HTML, CSS & JavaScript.

The Expandable Image Gallery contains a wide variety of content, including images, text, list groups, links, etc. The images are stacked vertically in narrow cards and depending on the screen size the image will appear. When you click on a narrow image it expands and the existing image will shrink.

In this blog (Expandable Image Gallery) on the webpage, there are five images where there is one expanded image and, four shrunk images. When you click on the shrunk image, the image will get expand. You can view all the shrunk images by clicking each of them. The expanded image contains some text about the image or the title of the image which is at the left bottom of the image. In this Expandable Image Gallery, you can give the image as much you need.

The source code of these Expandable Image Gallery using HTML, CSS & JavaScript is given below, if you want the source code of this program, you can copy it. You can use this code of Expandable Image Gallery with your creativity and, can take this project to the next level.

Expandable Image Gallery [Source Code]

To make this website, you would like to make three files: an HTML file, a CSS file & a JavaScript file. First, create an HTML file with the name of index.html, and remember, you have to create a file with a .html extension.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="style.css" /> <title>Expanding Image Cards || learningrobo</title> </head> <body> <div class="container"> <div class="panel active" style="background-image: url('https://cdn.pixabay.com/photo/2020/06/12/00/28/nature-5288669_1280.png')"> <h3>Explore The World</h3> <div class="credit">Made with <span style="color:tomato">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> </div> <div class="panel" style="background-image: url('https://cdn.pixabay.com/photo/2020/10/16/22/23/scenery-5660762__480.jpg')"> <h3>Forest House</h3> <div class="credit">Made with <span style="color:tomato">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> </div> <div class="panel" style="background-image: url('https://cdn.pixabay.com/photo/2020/05/25/13/38/watermelon-5218646__480.png')"> <h3>Watermelon</h3> <div class="credit">Made with <span style="color:tomato">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> </div> <div class="panel" style="background-image: url('https://cdn.pixabay.com/photo/2021/01/05/06/40/boat-5889919__480.png')"> <h3>Ocean</h3> <div class="credit">Made with <span style="color:tomato">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> </div> <div class="panel" style="background-image: url('https://cdn.pixabay.com/photo/2021/09/01/08/24/winter-6590867__480.png')"> <h3>Winter</h3> <div class="credit">Made with <span style="color:tomato">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> </div> </div> <script src="script.js"></script> </body> </html>
CSS provides style to an HTML page. To make the page attractive create a CSS file with the name style.css and, remember that you have to make a file with a .css extension.


@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');

* {
  box-sizing: border-box;
}

body {
  font-family: 'Muli', sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
background: -webkit-linear-gradient(to right, #38ef7d, #11998e);  
background: linear-gradient(to right, #38ef7d, #11998e); 

}

.container {
  display: flex;
  width: 100%;
  margin: 10px;
}

.panel {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 80vh;
  border-radius: 12px;
  color: #fff;
  cursor: pointer;
  flex: 0.5;
  margin: 10px;
  position: relative;
  -webkit-transition: all 700ms ease-in;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.panel h3 {
  font-size: 24px;
  position: absolute;
  bottom: 60px;
  left: 20px;
  margin: 0;
  opacity: 0;
  color: #000;
  background-color: #fff;
  padding: 5px;
  border-radius:5px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.panel.active {
  flex: 8;
}

.panel.active h3 {
  opacity: 1;
  transition: opacity 0.3s ease-in 0.4s;
}

.credit a{
    text-decoration: none;
    color: #000;
    text-decoration: underline;
  }

  .credit {
      color:#000;
      margin-top: 10px;
      opacity: 0;
      position: absolute;
      bottom: 20px;
      left: 20px;
  }

  .panel.active .credit {
    opacity: 1;
    transition: opacity 0.3s ease-in 0.4s;
  }
JavaScript makes the page work functionally which makes the image to be active on clicking. At last, create a JavaScript file with the name of script.js and, remember that you've got need to make a file with a .js extension.


const panels = document.querySelectorAll('.panel')

panels.forEach(panel => {
    panel.addEventListener('click', () => {
        removeActiveClasses()
        panel.classList.add('active')
    })
})

function removeActiveClasses() {
    panels.forEach(panel => {
        panel.classList.remove('active')
    })
}
We hope you would like these Expandable Image Gallery using HTML, CSS & JavaScript.

Thank you for reading our blog. If you face any problem in creating these Expandable Image Gallery using HTML, CSS & JavaScript, then contact us or comment us. We’ll try to provide a solution to your problem as soon as possible.

Thank you
Learning robo team

Post a Comment

Thank you
Learning robo team

Post a Comment (0)

Previous Post Next Post
Learning Robo says...
code copied
Welcome