How to Create Image Accordion using HTML and CSS?

In the domain of web design, discovering groundbreaking methods to exhibit images and other multimedia content is a pivotal facet of developing a captivating user experience. One popular technique that has gained significant traction in recent years is the image accordion, which allows users to easily navigate through a collection of images by expanding and collapsing individual sections. In this article, we will explore the steps required to create an image accordion using HTML and CSS, providing you with a simple yet effective way to enhance the visual appeal of your website. Whether you’re a seasoned web developer or just starting out, this tutorial will equip you with the knowledge and skills needed to implement this technique in your own projects.


An accordion is a user interface element that expands and collapses sections of content, often used to display frequently asked questions or other types of information in a compact and organized manner. It is possible to use images within an accordion element to create an image accordion.


To fashion an image accordion, a meticulous process is necessary involving HTML and CSS. Firstly, an encompassing element is required to accommodate the images, which will be located and sized by CSS. Next, you'll create a series of nested div elements for each image and set their dimensions and position within the container. Inside each nested div, you'll include an image tag with the source attribute set to the appropriate image file, as well as a caption element with the title of the image. You'll also need to set the visibility property of each nested div to hidden to hide the images until they are clicked.

To render the accordion interactive, it is essential to utilize JavaScript to register click events on each div that is nested. Once a div is clicked, its visibility property should be toggled to reveal or conceal the image and caption. Additionally, CSS is necessary to implement the accordion effect by adjusting the height of the clicked div to expand while simultaneously collapsing the other divs to produce a smooth sliding effect. To create an image accordion, one must possess knowledge of HTML, CSS, and JavaScript and have meticulous attention to detail in establishing the dimensions, placement, and visibility of each element. However, by mastering this method, one can enhance their website's visual appeal and incorporate an interactive feature that will captivate and astonish visitors.


The following is the complete code which we are going to have a look at in this example −

<!DOCTYPE html>
<html lang="en">
      body {
         height: 100vh;
         display: flex;
         justify-content: center;
         align-items: center;
      .main_box {
         width: 90%;
         height: 80vh;
         display: flex;
      .img {
         flex: 1;
         transform: skew(10deg);
         cursor: pointer;
         margin: 0 0.125em;
         transition: all .3s;
         background-position: center;
         border: 1px solid pink;
         position: relative;
      p {
         position: absolute;
         bottom: 0;
         left: 0;
         padding: .75em;
         background-color: rgba(0, 0, 0, 0.6);
         transform: rotate(-90deg);
         transform-origin: 0% 0%;
         transition: all 0.3s;
      .img1 {
         background-image: url(;
      .img2 {
         background-image: url(;
      .main_box:hover .img {
         transform: skew(0);
      .img:hover {
         flex: 5;
   <div class="main_box">
      <div class="img img1">
      <div class="img img2">


In summation, devising an image accordion utilizing HTML and CSS is a somewhat effortless procedure that can be executed by adhering to the guidelines stated in this write-up. With a modicum of perseverance and experience, you can attain an astonishing and aesthetically pleasing accordion effect on your website, captivating your guests with your expertise in web designing. Therefore, it is highly recommended to not falter in your endeavors to explore various design elements and techniques, so as to create a singular and unparalleled image accordion that can exhibit your artistic ingenuity and technical proficiency.

Updated on: 07-Sep-2023


Kickstart Your Career

Get certified by completing the course

Get Started