How to Build an Expandable Profile Card with HTML, CSS & JavaScript

Creating engaging user interfaces is essential for modern web applications. In this recipe, we will build a sleek, expandable Profile Card Component featuring smooth height transitions, animated button triggers, and dynamic social media stats.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Profile Card - Documented</title>
    
   <link rel="stylesheet" href="style.css" />
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@300;400;700&display=swap"
      rel="stylesheet"
    />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css"
    />
  </head>
  <body>
    <!-- Main Background Layout Wrapper -->
    <div class="container">
      <!-- Profile Card Outer Container -->
      <div class="card">
        
        <!-- Top Section: Contains Profile Avatar & Verification Badge -->
        <div class="card-top">
          <div class="card-top-img">
            <img src="images/model.png" alt="Profile Picture" />
            <div class="verified">
              <i class="fa-solid fa-check"></i>
            </div>
          </div>
        </div>

        <!-- Toggle Button: Triggers expansion when clicked -->
        <button class="card-btn">
          <span class="plus">+</span>
          <span class="btn-text">Contact Me</span>
        </button>

        <!-- Bottom Section: Contains Hidden User Details and Social Media Stats -->
        <div class="card-bottom">
          <h1>John Doe</h1>
          <h3>PRODUCT MANAGER</h3>
          
          <div class="social-media">
            <!-- LinkedIn Stats -->
            <div class="social-info">
              <i class="fa-brands fa-linkedin-in"></i>
              <span class="num">14.2K</span>
              <span class="followers">Followers</span>
            </div>
            <!-- Instagram Stats -->
            <div class="social-info">
              <i class="fa-brands fa-instagram"></i>
              <span class="num">23.3K</span>
              <span class="followers">Followers</span>
            </div>
            <!-- Facebook Stats -->
            <div class="social-info">
              <i class="fa-brands fa-facebook-f"></i>
              <span class="num">12.7K</span>
              <span class="followers">Followers</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  <script src="script.js"></script>
  </body>
</html>

      /* 
       * GLOBAL RESETS & STYLES
       * Resets browser defaults and sets 'Ubuntu' as the base font family.
       */
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: "Ubuntu", sans-serif;
      }

      /* Base font size conversion: 62.5% sets 1rem to 10px for easier REM calculations */
      html {
        font-size: 62.5%;
      }

      /* 
       * CONTAINER
       * Serves as the full-screen dark background canvas for the card component.
       */
      .container {
        width: 100%;
        height: 100vh;
        background-color: #131a24;
      }

      /* 
       * CARD COMPONENT (Collapsed State)
       * Sets default dimensions, background, shadow, and positioning.
       * 'transition: height 0.5s' ensures smooth expanding and collapsing animations.
       */
      .card {
        width: 35rem;
        height: 25rem;
        background-color: #323650;
        position: absolute;
        top: 15%;
        left: 50%;
        transform: translateX(-50%);
        border-radius: 1rem;
        box-shadow: 0 3rem 6rem rgba(0, 0, 0, 0.5);
        transition: height 0.5s;
      }

      /* 
       * MODIFIER CLASS (.change)
       * Expands the card height from 25rem to 60rem when active.
       */
      .change.card {
        height: 60rem;
      }

      /* Top section wrapper for profile picture */
      .card-top {
        height: 25rem;
        position: relative;
      }

      /* Profile image positioning container */
      .card-top-img {
        width: 12rem;
        height: 12rem;
        position: absolute;
        top: 5rem;
        left: 50%;
        transform: translateX(-50%);
      }

      /* Circular user profile avatar image */
      .card-top-img img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 50%;
        box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.3);
      }

      /* Verified badge with purple-pink gradient background */
      .verified {
        width: 2.5rem;
        height: 2.5rem;
        background: linear-gradient(to right, #a35ec6, #f35bbb);
        border-radius: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        bottom: 1rem;
        right: 0;
      }

      .verified i {
        font-size: 1.5rem;
        color: #fff;
      }

      /* 
       * INTERACTIVE ACTION BUTTON
       * Positioned at the boundary between top and bottom card sections.
       * Animates width expansion upon click.
       */
      .card-btn {
        width: 5rem;
        height: 4.5rem;
        background: linear-gradient(to right, #8262d2, #dc56b1);
        position: absolute;
        top: 22.5rem;
        left: 50%;
        transform: translateX(-50%);
        border: none;
        border-radius: 3rem;
        box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.3);
        cursor: pointer;
        transition: width 0.5s;
      }

      /* Expands button width when the card is open */
      .change .card-btn {
        width: 17rem;
      }

      /* Plus (+) icon inside button */
      .plus {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-size: 3rem;
        color: #fff;
        transition: all 0.5s;
      }

      /* Rotates plus icon into a close/cancel 'X' style icon when active */
      .change .plus {
        left: 1.5rem;
        transform: translateY(-50%) rotateZ(-45deg);
      }

      /* Hidden label inside button, fades in when card expands */
      .btn-text {
        font-size: 1.6rem;
        color: #fff;
        font-weight: bold;
        letter-spacing: 0.1rem;
        position: absolute;
        top: 50%;
        right: 3rem;
        transform: translateY(-50%);
        opacity: 0;
        visibility: hidden;
      }

      /* Fades in button label with transition delay */
      .change .btn-text {
        opacity: 1;
        visibility: visible;
        transition: all 0.5s 0.5s;
      }

      /* 
       * CARD BOTTOM SECTION
       * Holds user metadata and social media counters.
       */
      .card-bottom {
        height: 35rem;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-evenly;
        padding-top: 5rem;
      }

      /* User name title - hidden by default */
      .card-bottom h1 {
        font-size: 3rem;
        font-weight: 300;
        color: #fff;
        opacity: 0;
        visibility: hidden;
      }

      /* Job title subtitle - hidden by default */
      .card-bottom h3 {
        margin-top: -4rem;
        font-size: 1.2rem;
        font-weight: 300;
        color: #d1d1d1;
        letter-spacing: 0.1rem;
        opacity: 0;
        visibility: hidden;
      }

      /* Fades in user name and title when card expands */
      .change .card-bottom h1 {
        opacity: 1;
        visibility: visible;
        transition: all 0.5s 0.5s;
      }

      .change .card-bottom h3 {
        opacity: 1;
        visibility: visible;
        transition: all 0.5s 0.6s;
      }

      /* Container for social media metrics */
      .social-media {
        width: 100%;
        display: flex;
        justify-content: space-evenly;
      }

      /* Individual social metric blocks */
      .social-info {
        display: flex;
        flex-direction: column;
        align-items: center;
        opacity: 0;
        visibility: hidden;
      }

      /* Staggered entrance animations for social media items using transition-delay */
      .change .social-info:nth-child(1) {
        opacity: 1;
        visibility: visible;
        transition: all 0.5s 0.8s;
      }

      .change .social-info:nth-child(2) {
        opacity: 1;
        visibility: visible;
        transition: all 0.5s 0.9s;
      }

      .change .social-info:nth-child(3) {
        opacity: 1;
        visibility: visible;
        transition: all 0.5s 1s;
      }

      /* Round social media icon styling */
      .social-info i {
        width: 6rem;
        height: 6rem;
        background-color: #ccc;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.3);
        font-size: 1.8rem;
        color: #fff;
      }

      /* Specific brand gradient backgrounds for social platform icons */
      .social-info:nth-child(1) i {
        background: linear-gradient(to right, #0077bb, #51b0e7); /* LinkedIn */
      }

      .social-info:nth-child(2) i {
        background: linear-gradient(to right, #833ab4, #c13584, #e1306c); /* Instagram */
      }

      .social-info:nth-child(3) i {
        background: linear-gradient(to right, #4267b2, #5b8df1); /* Facebook */
      }

      /* Follower count numbers and labels */
      .num {
        font-size: 1.8rem;
        font-weight: bold;
        color: #eee;
        margin: 1.5rem 0 0.5rem 0;
      }

      .followers {
        color: #aaa;
      }

      /* 
       * SELECT DOM ELEMENTS
       * Retrieves the interactive button (.card-btn) and card container (.card).
       */
      const button = document.querySelector(".card-btn");
      const card = document.querySelector(".card");

      /* 
       * EVENT LISTENER
       * Toggles the 'change' class on the card element upon button click.
       * This triggers all defined CSS transition animations simultaneously.
       */
      button.addEventListener("click", () => {
        card.classList.toggle("change");
      });

🍳 Overview & How It Works

This project creates a interactive UI widget using standard web technologies:

  1. HTML Structure (index.html): Defines the component into card top (profile image & verified badge), card button trigger, and card bottom (user information & social media stats).
  2. CSS Styling & Animations (style.css):
    • Sets up a modern dark theme background and gradient accents.
    • Utilizes CSS transition properties (transition, opacity, transform) so elements smooth into view when expanded.
    • Uses .change class modifier rules to control expanded layout states (e.g., expanding card height from 25rem to 60rem, rotating the + icon, expanding the button width, and fading in user details with staggered delays).
  3. JavaScript Interactivity (script.js):
    • Listens for click events on the main action button (.card-btn).
    • Toggles the .change class on the .card element dynamically, seamlessly triggering the CSS animation transitions.

📝 Customisable Variables & Styling Guide

Here are the key CSS properties and variables you can modify in style.css to fit your project branding:

1. Card Dimensions & Timing

  • Initial & Expanded Height:
    • .card { height: 25rem; } — collapsed height.
    • .change.card { height: 60rem; } — expanded height.
  • Transition Durations:
    • .card { transition: height 0.5s; } — smooth expansion speed.
    • .change .social-info:nth-child(x) — adjust the staggered entry timings (0.8s, 0.9s, 1s) to speed up or slow down reveal animations.

2. Colors & Gradients

  • Background Theme:
    • .container { background-color: #131a24; } — page background.
    • .card { background-color: #323650; } — main card surface background.
  • Gradients:
    • .verified: linear-gradient(to right, #a35ec6, #f35bbb) — badge accent.
    • .card-btn: linear-gradient(to right, #8262d2, #dc56b1) — button styling.
    • .social-info:nth-child(...) i: Individual gradient themes for LinkedIn, Instagram, and Facebook icons.

3. Typography & Assets

  • Font Family: Defined globally on * { font-family: "Ubuntu", sans-serif; }.
  • Profile Picture: Replace "images/model.png" inside index.html with your custom image path.

Tinggalkan komentar