An image filter gallery is a popular design element used on many modern websites to showcase a collection of images. It allows users to filter images based on specific categories. A responsive image filter gallery, on the other hand, is a gallery that adjusts its size and layout based on the screen size, making it accessible on all devices. In this article, we will guide you on how to create a responsive image filter gallery using Bootstrap and JavaScript.
Step 1: Create the HTML Markup
The first step is to create the HTML markup for the image filter gallery. You can use the div tag with a unique class or ID to group the images to be displayed. Each image can be represented by a div with its unique class or ID. You can add images and any other content inside each image div.
Image Gallery with Filter | WPHotshot
Nature 1
Lorem ipsum dolor..
Nature 2
Lorem ipsum dolor..
Nature 3
Lorem ipsum dolor..
Car 1
Lorem ipsum dolor..
Car 2
Lorem ipsum dolor..
Car 3
Lorem ipsum dolor..
People 1
Lorem ipsum dolor..
People 2
Lorem ipsum dolor..
Step 2: Add Bootstrap Styling
Next, you can add Bootstrap styling to the image filter gallery to make it responsive. You can use Bootstrap’s grid system to create a layout that adjusts based on the screen size. You can also use Bootstrap’s built-in classes, such as img-fluid and card, to style the images and their containers.
/* Import Google font - Poppins */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');
* {
font-family: "Poppins", sans-serif;
}
body {
background: linear-gradient(to right, #2832d4, #00A5B2, #7209D4);
}
body .container {
max-width: 1100px;
}
#filter-buttons button {
border-radius: 3px;
background: #2832d4;
border-color: transparent;
color: #fff;
}
#filter-buttons button:hover {
background: #00A5B2;
}
#filter-buttons button.active {
color: #fff;
background: #00A5B2;
}
#filterable-cards .card {
width: 15rem;
border: 2px solid transparent;
}
#filterable-cards .card.hide {
display: none;
}
@media (max-width: 600px) {
#filterable-cards {
justify-content: center;
}
#filterable-cards .card {
width: calc(100% / 2 - 10px);
}
}
.card img{
border-radius: 5px 5px 0 0;
}
Step 3: Implement JavaScript Filter Logic
The most crucial step in creating an image filter gallery is implementing the JavaScript filter logic. You can use JavaScript to create a function that will filter the images based on specific categories. You can do this by adding event listeners to the filter buttons and using JavaScript’s built-in filter method to show or hide images based on their categories.
// Select relevant HTML elements
const filterButtons = document.querySelectorAll("#filter-buttons button");
const filterableCards = document.querySelectorAll("#filterable-cards .card");
// Function to filter cards based on filter buttons
const filterCards = (e) => {
document.querySelector("#filter-buttons .active").classList.remove("active");
e.target.classList.add("active");
filterableCards.forEach(card => {
// show the card if it matches the clicked filter or show all cards if "all" filter is clicked
if(card.dataset.name === e.target.dataset.filter || e.target.dataset.filter === "all") {
return card.classList.replace("hide", "show");
}
card.classList.add("hide");
});
}
filterButtons.forEach(button => button.addEventListener("click", filterCards));
Conclusion
In conclusion, an image filter gallery is a powerful design element that can make your website more engaging and interactive. By following the steps above, you can create a responsive image filter gallery using Bootstrap and JavaScript that will showcase your images in a visually appealing and interactive way. Remember to test your code thoroughly to ensure that it works as intended. Implementing an image filter gallery on your website can improve user engagement and ultimately lead to higher conversion rates.