By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > HTML & CSS > Card Design > How to Make Expanding Cards In HTML, CSS, and JavaScript
Card DesignHTML & CSSJavaScript

How to Make Expanding Cards In HTML, CSS, and JavaScript

Admin
Last updated: 2022/12/03 at 4:30 AM
Admin
Share
6 Min Read
Expanding Cards

In this article, we will Make Expanding Cards using HTML, CSS, and JavaScript. We will be adding some images in this, and we will add them as shrunk and small version of the screen using CSS Styling, and lastly we will add expanding functioning to it using JavaScript. This will be very easy and beginner-friendly, so let’s make it step-by-step.

Contents
Pre-requisites to Make Expanding Cards using HTML, CSS, and JavaScriptCreating HTML Markup and Adding ImageCustomizing The ImagesAdding ResponsivenessAdding Functionality to ImagesFull Source Code of Expanding Cards using HTML, CSS, and JavaScriptOutputYou may also like:
Demo

Pre-requisites to Make Expanding Cards using HTML, CSS, and JavaScript

  • Basic knowledge of HTML.
  • Good knowledge of CSS.
  • Good knowledge of JavaScript.

Creating HTML Markup and Adding Image

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>My Project</title>
</head>
<body>
    <div class="container">
        <div class="panel active" style="background-image: url('https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');">
            <h3>Explore The World</h3>
        </div>

        <div class="panel"
            style="background-image: url('https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');">
            <h3>Wild Forest</h3>
        </div>

        <div class="panel"
            style="background-image: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80');">
            <h3>Sunny Beach</h3>
        </div>

        <div class="panel"
            style="background-image: url('https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80');">
            <h3>City on Nature</h3>
        </div>

        <div class="panel"
            style="background-image: url('https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');">
            <h3>Mountains - Clouds</h3>
        </div>
    </div>

    <script src="script.js"></script>
</body>
</html>

In HTML, we have linked our CSS file for styling, and we have also linked a script file for adding functionality. Now we have added a div container in which we will add our images. Then we have added multiple divs for panel for each image. In these panels we will add a text and image will be added as background image for these tags. Here we have added 5 images and one of them we made added active class. We will define these active classes in our CSS to expand our images.

 Expanding Cards

Customizing The Images

@import url('https://fonts.googleapis.com/css2?Muli&display=swap');

*{
    box-sizing: border-box;
}

body{
    font-family: 'Muli', sans-serif;
    margin: 0;
    display:flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    overflow:hidden;
}

.container{
    display:flex;
    width:90vw;
}

.panel{
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    height:80vh;
    border-radius: 50px;
    flex:0.5;
    position:relative;
    transition: flex 0.5s ease-in;
    color:#fff;
    cursor:pointer;
    margin:10px;
}

.panel h3{
    position:absolute;
    bottom:20px;
    left:20px;
    opacity:0;
}

.panel.active{
    flex:5;
}

.panel.active h3{
    opacity:1;
     transition: opacity 0.4s ease-in 0.4s;
}

Now in CSS, we have removed our default margin and padding, then we have centered our content using some properties like align-items and justify-content properties. Also, we have fixed some height to our content. After that, we have extended our image height not width, so that it will be stretched vertically. Also, we have added flex to 0.5 so that image will be fit according to text width. Then we have added some transition on these cards, with this we will get some animation when we extend the image.

Now in active class we just need to extend value of flex, we have made flex 0.5 to 5, so width will be extended.

 Expanding Cards

Adding Responsiveness

With the help of below code, we have added responsiveness where we have maximum width of 480px.

@media(max-width:480px){
    .container{
        width:100vw;
    }

    .panel:nth-of-type(4),.panel:nth-of-type(5){
        display:none;
    }
}

Adding Functionality to Images

const panels = document.querySelectorAll('.panel');

panels.forEach(panel => {
    panel.addEventListener('click', () => {
        removeActiveClasses();
        panel.classList.add('active');
    })
})

function removeActiveClasses(){
    panels.forEach(panel => {
        panel.classList.remove('active');
    })
}

In the JavaScript part, we have fetched panel classes in which we have added our images using the querySelectorAll method. Then we have added a loop for each of the panel, in these panels we have added a click event, in this we have called a function. In this function, we are removing active class using removeActiveClasses() function. And we have added active class on clicked panel.

 Expanding Cards

Full Source Code of Expanding Cards using HTML, CSS, and JavaScript

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>My Project</title>
</head>
<body>
    <div class="container">
        <div class="panel active" style="background-image: url('https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');">
            <h3>Explore The World</h3>
        </div>

        <div class="panel"
            style="background-image: url('https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');">
            <h3>Wild Forest</h3>
        </div>

        <div class="panel"
            style="background-image: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80');">
            <h3>Sunny Beach</h3>
        </div>

        <div class="panel"
            style="background-image: url('https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80');">
            <h3>City on Nature</h3>
        </div>

        <div class="panel"
            style="background-image: url('https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');">
            <h3>Mountains - Clouds</h3>
        </div>
    </div>

    <script src="script.js"></script>
</body>
</html>

style.css

@import url('https://fonts.googleapis.com/css2?Muli&display=swap');

*{
    box-sizing: border-box;
}

body{
    font-family: 'Muli', sans-serif;
    margin: 0;
    display:flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    overflow:hidden;
}

.container{
    display:flex;
    width:90vw;
}

.panel{
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    height:80vh;
    border-radius: 50px;
    flex:0.5;
    position:relative;
    transition: flex 0.5s ease-in;
    color:#fff;
    cursor:pointer;
    margin:10px;
}

.panel h3{
    position:absolute;
    bottom:20px;
    left:20px;
    opacity:0;
}

.panel.active{
    flex:5;
}

.panel.active h3{
    opacity:1;
     transition: opacity 0.4s ease-in 0.4s;
}

@media(max-width:480px){
    .container{
        width:100vw;
    }

    .panel:nth-of-type(4),.panel:nth-of-type(5){
        display:none;
    }
}

script.js

const panels = document.querySelectorAll('.panel');

panels.forEach(panel => {
    panel.addEventListener('click', () => {
        removeActiveClasses();
        panel.classList.add('active');
    })
})

function removeActiveClasses(){
    panels.forEach(panel => {
        panel.classList.remove('active');
    })
}

Output

 Expanding Cards
Demo
Download Code

Check out video reference here:

You may also like:

  • How To Build a Quiz App With HTML CSS and JavaScript
  • How To Make Slide Puzzle Game in HTML CSS and JavaScript
  • How To Make Space War Game Using HTML, CSS & JavaScript

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: Animation, Expanding Cards, UI Design
Share this Article
Facebook Twitter Email Print
What do you think?
Love1
Sad0
Happy1
Sleepy0
Angry0
Dead0
Wink1
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Popup Login Form How to Make Popup Login Form in HTML, CSS & JavaScript
Next Article Full Screen Image Slider How to Make Full Screen Image Slider in JavaScript
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?