How to preview Image on click in Gallery View using HTML, CSS and JavaScript ? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 3 Likes Like Report In this article, we see how easily we can create an Image Gallery with a preview feature using HTML, CSS, and some JavaScript.ApproachCreate a div with a class container.Create two more div inside the first div one for the main view and the other for the side view with classes main_view and side_view.Inside the main view insert one image tag with the id main.Inside the side view insert all other images of the gallery with function change and pass the src of the image to function.Give width and margin to a container.Give the height and width to a main_view.Set the dimensions of the image in the main_view.Set side_view to display all images in proper dimensions using flex.Using the change function we will just replace the src of main_view with the source of the clicked image.Example: This example shows the implementation of the above-explained approach. HTML <!DOCTYPE html> <html> <body> <!-- Container for our gallery --> <div class="container"> <!-- Main view of our gallery --> <div class="main_view"> <img src="one.jpg" id="main" alt="IMAGE"> </div> <!-- All images with side view --> <div class="side_view"> <img src="one.jpg" onclick="change(this.src)"> <img src="two.jpg" onclick="change(this.src)"> <img src="three.jpg" onclick="change(this.src)"> <img src="four.jpg" onclick="change(this.src)"> </div> </div> </body> </html> CSS /*Setting Basic Dimensions to give gallery view */ .container { margin: 0 auto; width: 90%; } .main_view { width: 80%; height: 25rem; } .main_view img { width: 100%; height: 100%; object-fit: cover; } .side_view { display: flex; justify-content: center; flex-wrap: wrap; } .side_view img { width: 9rem; height: 7rem; object-fit: cover; cursor: pointer; margin: 0.5rem; } JavaScript const change = src => { document.getElementById('main').src = src } Output: Create Quiz How to preview Image on click in Gallery View using HTML, CSS and JavaScript ? Comment N NANDINIJAIN Follow 3 Improve N NANDINIJAIN Follow 3 Improve Article Tags : JavaScript Web Technologies JavaScript-Questions JavaScript-Projects Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like