How to create Expanding Cards using HTML, CSS and Javascript ? Last Updated : 18 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Creating expanding cards using HTML, CSS, and JavaScript involves creating a set of cards that expand when clicked.ApproachSelection of Sections:The code starts by selecting all HTML elements with the class 'section' using the document.querySelectorAll('.section') method.This creates a NodeList containing all elements with the specified class.Event Listener for Each Section:A forEach the loop is used to iterate over each section in the NodeList (sections).For each section, an event listener for the 'click' event is added.Removing 'active' Class from Other Sections:Inside the click event handler, another forEach the loop is used to iterate over all sections (sections).For each section in this loop, the 'active' class is removed using section.classList.remove('active').Adding 'active' Class to the Clicked Section:After removing the 'active' class from all sections, the 'active' class is added to the currently clicked section using section.classList.add('active').Example: We are following the above-explained approach. HTML <!DOCTYPE html> <html lang="en"> <head> <style> .container{ display: flex; width: 80%; } .one{ background: url(img/one.jpg); } .two{ background: url(img/two.jpg); } .three{ background: url(img/three.jpg); } .four{ background: url(img/four.jpg); } .section{ background-size: cover; background-position: center; background-repeat: no-repeat; height: 80vh; cursor: pointer; flex: 0.2; margin:8px; position: relative; transition: all 0.7s ease-out; } .section.active{ flex: 3; } </style> </head> <body> <!-- Container --> <div class="container"> <!-- Div with section and active --> <div class="section one active"></div> <!-- All another div with section --> <div class="section two"></div> <div class="section three"></div> <div class="section four"></div> </div> <script> const sections = document.querySelectorAll('.section') sections.forEach((section)=>{ section.addEventListener('click',()=>{ sections.forEach((section) => { section.classList.remove('active') }) section.classList.add('active') }) }) </script> </body> </html> Output Comment More info N NANDINIJAIN Follow Improve Article Tags : JavaScript JavaScript-Questions 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 Strings6 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)9 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 readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like