How To Create an Image Accordion Using CSS

How To Create an Image Accordion Using CSS


Welcome to my website U-GINE MEDIA. In this website I teach tutorials and share source code on some programming (HTML, CSS & JavaScript) tutorials. 

Before we get started, do well to subscribe to my channel to never miss out on any update that I post every single day.

In this tutorial topic "How To Create an Image Accordion Using CSS & JavaScript", we will learn how to create it following these simple easy steps.

Share this to your programmer friends. If y'all learnt something today, make sure to let me in the comments. 

If interested in watching the video before running the code below, you can click the play button to get started.


Code Begins

  1. First, create a folder with any name you like. Then, make the necessary files inside it.
  2. Create a file called index.html to serve as the main file.
  3. Create a file called style.css for the CSS code.
  4. Finally, download the Images folder and put it in your project directory. This folder contains default showcase images for the website. You can also use your own images.

To start, add the following HTML codes to your index.html file. These codes include essential HTML markup with different semantic tags, such as div, form, input, button, image, etc., to build the website layout. 


<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="index.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Accordion Image Slider</title>
</head>
<body>


    <div class="accordion">
        <ul>
            <li>
                <div>
                    <a href="#">
                    <h2>Apple Fruit</h2>
                    <p>Apple is one of my favorite fruit.</p>
                    </a>
                </div>
            </li>
            <li>
                <div>
                    <a href="#">
                    <h2>Banana Fruit</h2>
                    <p>I Love Eating Banana A lot.</p>
                    </a>
                </div>
            </li>
            <li>
                <div>
                    <a href="#">
                    <h2>Blue Guava</h2>
                    <p>This fruit is found in the blue ocean.</p>
                    </a>
                </div>
            </li>
        </ul>
    </div>

    
    
</body>
</html>

Finally, add the following CSS codes to your style.css file to make your AI image generator website beautiful and user-friendly. You can customize the different CSS properties, such as color, background, font, etc., to give a personalized touch to your website. 


body {
    background: #042104;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.accordion {
    width: 80%;
    height: 300px;
    overflow: hidden;
    border-radius: 10px;
}

ul {
    width: 100%;
    display: table;
    table-layout: fixed;
    margin: 0;
    padding: 0;
}

li {
    --img-1: url("img1.jpg");
    --img-2: url("img2.jpg");
    --img-3: url("img3.jpg");

    width: 30%;
    height: 300px;
    display: table-cell;
    vertical-align: bottom;
    position: relative;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    transition: all 500ms ease;

    &:nth-child(1) {
        background-image: var(--img-1);
    }
    &:nth-child(2) {
        background-image: var(--img-2);
    }
    &:nth-child(3) {
        background-image: var(--img-3);
    }
}

li div {
    display: block;
    overflow: hidden;
    width: 100%;
}

li div a {
    display: block;
    height: 300px;
    width: 100%;
    color: #fff;
    position: relative;
    z-index: 3;
    padding: 15px 20px;
    box-sizing: border-box;
    text-decoration: none;
    font-family: Open Sans, sans-serif;
    transition: all 200ms ease;
}

li div a * {
    width: 100%;
    margin: 0;
    text-overflow: ellipsis;
    position: relative;
    z-index: 5;
    white-space: nowrap;
    overflow: hidden;
    transform: translateX(-20px);
    transition: all 400ms ease;
}

li div a h2 {
    font-family: Montserrat, sans-serif;
    text-overflow: clip;
    font-size: 2.5em;
    text-transform: uppercase;
    margin-bottom: 2px;
    top: 160px;
}

li div a p {
    top: 160px;
    font-size: 1.5em;
}

li div a * {
    opacity: 0;
}

ul:hover li {
    width: 10%;
}

ul:hover li:hover {
    width: 80%;
}

ul:hover li:hover a {
    background: #00000066;
}

ul:hover li:hover a * {
    opacity: 1;
    transform: translateX(0);
}


Conclusion and Final word

Now, we have reached the end of our code, for any information, you can comment any problem or code-not-working issues to resolve it, am always with you. You can also visit my YouTube Channel for more updates on HTML, CSS & JavaScript Designs. I believe you can create "an Image Accordion Using CSS & JavaScript". and do so much more with codes. Go extremely far in exploring the world of HTML, CSS & JavaScript, its gonna be fun, trust me 😃.

Happy Coding Journey 🚀

Post a Comment

0 Comments

Close Menu