
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to create a tabbed image gallery with CSS and JavaScript?
Following is the code to produce a tabbed image gallery with CSS and JavaScript −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> * { box-sizing: border-box; } h1 { text-align: center; } .ImgThumbnail { border-radius: 5px; cursor: pointer; transition: 0.3s; height: 250px; width: 250px; } .ImgThumbnail:nth-of-type(1) { margin-left: 20%; } .modal { display: none; position: relative; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 60%; overflow: auto; } .modalImage { margin: auto; display: block; width: 50%; height: 60%; max-width: 700px; } #caption { margin: auto; display: block; width: 80%; max-width: 700px; text-align: center; color: #ccc; padding: 10px 0; height: 150px; } .close { font-weight: bolder; position: absolute; right: 25%; color: #ffffff; font-size: 40px; transition: 0.3s; } .close:hover, .close:focus { color: rgb(255, 0, 0); cursor: pointer; } @media only screen and (max-width: 700px) { .modalImage { width: 100%; } } </style> </head> <body> <h1>Image Modal Gallery Example</h1> <img class="ImgThumbnail" src="https://images.pexels.com/photos/3635300/pexels-photo-3635300.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"/> <img class="ImgThumbnail" src="https://i.picsum.photos/id/237/536/354.jpg"/> <img class="ImgThumbnail" src="https://images.pexels.com/photos/3802510/pexels-photo-3802510.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"/> <div class="modal"> <span class="close">×</span> <img class="modalImage" id="img01" /> </div> <script> var modalEle = document.querySelector(".modal"); var modalImage = document.querySelector(".modalImage"); Array.from(document.querySelectorAll(".ImgThumbnail")).forEach(item => { item.addEventListener("click", event => { modalEle.style.display = "block"; modalImage.src = event.target.src; }); }); document.querySelector(".close").addEventListener("click", () => { modalEle.style.display = "none"; }); </script> </body> </html>
Output
The above code will produce the following output −
- Related Articles
- How to create a modal image gallery with CSS and JavaScript?
- How to create a responsive image gallery with CSS
- How to create an image gallery with CSS
- How to Create Image Lightbox Gallery using HTML CSS and JavaScript?
- How to create a responsive slideshow gallery with CSS and JavaScript?
- How to Create a Tab Image Gallery?
- How to create a responsive portfolio gallery grid with CSS?
- How to create an image to zoom with CSS and JavaScript?
- How to create Portfolio Gallery using the HTML and CSS
- How to create a "black and white" image with CSS?
- How to create a Hero Image with CSS?
- How to create a responsive image with CSS?
- How to create a sticky image with CSS?
- Create a tabbed menu with Bootstrap
- How to create image filters with CSS

Advertisements