How to Create Expanding Cards using HTML, CSS, and JavaScript? | InternPedia

0

 


 Creating expanding cards using HTML, CSS, and JavaScript involves creating a set of cards that expand when clicked, enhancing interactivity and providing a dynamic visual experience.

HTML Code:

index.html
// www.internpedia.in
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Expanding Cards</title>
  </head>
  <body>
    <div class="container">
      <div class="panel active" style="background-image: url('1.jpg')">
        <h3>World</h3>
      </div>
      <div class="panel" style="background-image: url('2.jpg')">
        <h3>Beach</h3>
      </div>
      <div class="panel" style="background-image: url('3.jpg')">
        <h3>Forest</h3>
      </div>
      <div class="panel" style="background-image: url('4.jpg')">
        <h3>City</h3>
      </div>
      <div class="panel" style="background-image: url('5.jpg')">
        <h3>Mountains</h3>
      </div>

    </div>
  </body>
</html>

CSS Code:

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

* {
  box-sizing: border-box;
}
body {
  font-family: 'Muli', sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}
.container {
  display: flex;
  width: 90vw;
}
.panel {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 80vh;
  border-radius: 50px;
  color: #fff;
  cursor: pointer;
  flex: 0.5;
  margin: 10px;
  position: relative;
  -webkit-transition: all 700ms ease-in;
}
.panel h3 {
  font-size: 24px;
  position: absolute;
  bottom: 20px;
  left: 20px;
  margin: 0;
  opacity: 0;
}
.panel.active {
  flex: 5;
}
.panel.active h3 {
  opacity: 1;
  transition: opacity 0.3s ease-in 0.4s;
}
@media (max-width: 480px) {
  .container {
    width: 100vw;
  }
  .panel:nth-of-type(4),
  .panel:nth-of-type(5) {
    display: none;
  }
}

JavaScript Code:

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')
    })
}

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !