How to Create an Image Gallery with a Horizontal Scrollbar using CSS? Last Updated : 30 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Image galleries are essential tools for showcasing multiple images within a single container on a website. They allow users to view a collection of images in an organized way. When we use the horizontal scrollbar, then we can reduce the space. In this tutorial, we will go through the steps on how to create an image gallery with a horizontal scrollbar using CSS.PreviewPreviewApproachFirst, create a basic HTML layout for your image gallery and modal pop-ups. Each image in the gallery should have a corresponding modal with a unique ID.Then add image elements inside the gallery div with "src", "alt", and "onclick()" attributes to trigger the modal.Then use CSS to style your webpage, including elements like image containers, galleries, modals, and layout components.For responsive model images, use the media queries in CSS. This ensures that your images adapt well to different screen sizes and devices.Implement JavaScript functions to handle the opening and closing of modal windows when images are clicked. When a user clicks on an image, fetch the respective modal ID and set its display property to "block". Similarly, clicking the close button should set the display property of the modal to "none".Example: The example shows how to create an image gallery with a horizontal scrollbar using CSS. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Responsive Modal Images</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1 class="main-heading"> Image gallery with a horizontal scrollbar using CSS</h1> <div class="gallery"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240308154939/html-(1).jpg" alt="HTML" class="gallery-image" onclick="openModal('modal1', 1)"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240308154940/js-(1).jpg" alt="JavaScript" class="gallery-image" onclick="openModal('modal2', 2)"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240308154942/web-(1).jpg" alt="Web" class="gallery-image" onclick="openModal('modal3', 3)"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240308154945/web2-(1).jpg" alt="Web 2" class="gallery-image" onclick="openModal('modal4', 4)"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240322101847/Default_An_illustration_depictin-(2)-660.jpg" alt="Cyber security" class="gallery-image" onclick="openModal('modal5', 5)"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20230416201559/DevOps-Roadmap.webp" alt="Devops" class="gallery-image" onclick="openModal('modal6', 6)"> </div> <div id="modal1" class="modal"> <span class="close" onclick="closeModal('modal1')">×</span> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240308154939/html-(1).jpg" alt="HTML" class="modal-content"> <div class="message"></div> </div> <div id="modal2" class="modal"> <span class="close" onclick="closeModal('modal2')">×</span> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240308154940/js-(1).jpg" alt="JavaScript" class="modal-content"> <div class="message"></div> </div> <div id="modal3" class="modal"> <span class="close" onclick="closeModal('modal3')">×</span> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240308154942/web-(1).jpg" alt="Web" class="modal-content"> <div class="message"></div> </div> <div id="modal4" class="modal"> <span class="close" onclick="closeModal('modal4')">×</span> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240308154945/web2-(1).jpg" alt="Web 2" class="modal-content"> <div class="message"></div> </div> <div id="modal5" class="modal"> <span class="close" onclick="closeModal('modal5')">×</span> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240322101847/Default_An_illustration_depictin-(2)-660.jpg" alt="Cyber security" class="modal-content"> <div class="message"></div> </div> <div id="modal6" class="modal"> <span class="close" onclick="closeModal('modal6')">×</span> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20230416201559/DevOps-Roadmap.webp" alt="Devops" class="modal-content"> <div class="message"></div> </div> <script> function openModal(modalId, index) { let modal = document.getElementById(modalId); modal.style.display = "flex"; modal.classList.add("show"); let message = modal.querySelector(".message"); message.innerText = `You clicked on the ${index} image.`; } function closeModal(modalId) { let modal = document.getElementById(modalId); modal.classList.remove("show"); setTimeout(function () { modal.style.display = "none"; modal.querySelector(".message").innerText = ""; }, 300); } </script> </body> </html> CSS * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: Arial, sans-serif; background: #12dea4; } .main-heading { color: #2051e6; text-align: center; margin: 20px 0; } .gallery { display: flex; overflow-x: auto; white-space: nowrap; padding: 10px; margin: 0 20px; scrollbar-width: thin; scrollbar-color: #2051e6 #12dea4; } .gallery-image { display: inline-block; width: auto; height: 150px; margin-right: 10px; cursor: pointer; transition: transform 0.3s ease; } .gallery-image:last-child { margin-right: 0; } .gallery-image:hover { transform: scale(1.1); } .modal { display: none; position: fixed; z-index: 999; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.7); align-items: center; justify-content: center; opacity: 0; transition: opacity 0.3s ease; } .modal-content { position: relative; width: 90%; height: 90%; max-width: 500px; max-height: 80%; border-radius: 5px; overflow: hidden; animation: fadeIn 0.5s ease forwards; } @keyframes fadeIn { from { opacity: 0; transform: scale(0.8); } to { opacity: 1; transform: scale(1); } } .modal.show { display: flex; opacity: 1; } .close { position: absolute; top: 10px; right: 10px; color: #fff; font-size: 24px; cursor: pointer; transition: transform 0.3s ease; } .close:hover { transform: rotate(45deg); } .message { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); background-color: rgba(13, 219, 6, 0.7); color: #fff; padding: 5px 10px; border-radius: 5px; } @media screen and (max-width: 768px) { .gallery-image { height: 100px; } } @media screen and (max-width: 480px) { .gallery-image { height: 80px; } } Output: Comment More infoAdvertise with us S skaftafh Follow Improve Article Tags : Web Technologies CSS Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Like