Filterable Gallery Using HTML, CSS and JavaScript

Hello wizards, I hope you are doing great. Today in this blog you’ll learn how to create the "Filterable Gallery" project using HTML, CSS & JavaScript. 

Preview 

In the above video, you’ve seen the preview of the "Filterable Gallery" project and I hope now you are able to create this type of project. If not, I have provided all the HTML CSS and JavaScript code below. 

Filterable Gallery [Source Code] 

To get the following HTML, CSS & JS code for the Filterable Gallery project. You need to create three files one is a HTML file, second one is a CSS file and the another one is JS file. After creating these three files then you can copy-paste the given codes on your document. 

Remember, you’ve to create a file with .html extension for HTML code, .css extension for CSS code and .js for JavaScript code.

You can also download all source code files from the given download button.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Filterable Gallery</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="container">
        <h1>Filterable Gallery</h1>
        <div class="menu">
            <button class="btn active" data-name="all">All</button>
            <button class="btn" data-name="men">Men</button>
            <button class="btn" data-name="women">Women</button>
            <button class="btn" data-name="children">Children</button>
        </div>

        <div class="items">
            <div class="item men">
                <img src="images/men1.jpg" alt="men">
            </div>
            <div class="item men">
                <img src="images/men2.jpg" alt="men">
            </div>
            <div class="item women">
                <img src="images/women1.jpg" alt="men">
            </div>
            <div class="item women">
                <img src="images/women2.jpg" alt="men">
            </div>
            <div class="item children">
                <img src="images/children1.jpg" alt="men">
            </div>
            <div class="item children">
                <img src="images/children2.jpg" alt="men">
            </div>
        </div>
    </div>

    <script src="index.js"></script>
</body>

</html>
CSS
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap");

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}

body {
  background-color: black;
  color: white;
}

.container {
  max-width: 1200px;
  margin: 100px auto;
  text-align: center;
  padding: 10px;
}

.container h1 {
  font-size: 30px;
  text-transform: uppercase;
  letter-spacing: 4px;
  word-spacing: 5px;
  margin-bottom: 10px;
}

.container .menu {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 0 20px;
  margin-bottom: 25px;
}

.container .menu .btn {
  font-size: 15px;
  font-weight: 500;
  background-color: transparent;
  color: white;
  border: none;
  padding: 10px 20px;
  border-bottom: 3px solid transparent;
  transition: border-bottom 0.5s ease-in-out;
  cursor: pointer;
}

.container .menu .btn:hover {
  border-bottom: 3px solid white;
}

.container .menu .btn.active {
  border-bottom: 3px solid white;
}

.container .items {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  justify-content: center;
  align-items: center;
}

.container .items .item {
  width: 270px;
  height: 200px;
}

.container .items .item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border: 3px solid white;
  border-radius: 10px;
}
JavaScript
const btnEl =document.querySelectorAll(".btn");
const itemEls =document.querySelectorAll(".item");
var tempActive = document.querySelector(".active");

btnEl.forEach((btn)=>{
    btn.addEventListener("click",(e)=>{

        // To add border to button 
        tempActive.classList.remove("active")
        btn.classList.add("active")
        tempActive = btn;

        // To filter the items with item name
        const name = e.target.dataset.name;
        itemEls.forEach((itemEl)=>{
            if(name==="all"){
                itemEl.style.display = "block"
            }else{
                if(itemEl.classList.contains(name)){
                    itemEl.style.display = "block"
                }else{
                    itemEl.style.display = "none";
                }
            }
        })
    })
})
You have to wait 15 seconds.

Generating Download Link...

Post a Comment

Previous Post Next Post