skip to Main Content

I’m trying to create a home page where there are 6 buttons and when a user pass the cursor hover them they shrink and a text expand from the center of each button.

I managed to get this:

body {
  background-color: white;
}

.button-container {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.image-button {
  position: relative;
  overflow: hidden;
  width: 10vw;
  height: 10vw;
  background: none;
  border: none;
}

.image-button img {
  width: 100%;
  height: 100%;
  transition: all 0.5s ease;
}

.image-button:hover img {
  transform: scale(0.6); 
}

.button-container span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 8vh;
  transition: font-size 0.3s ease;
  opacity: 0;
  z-index: 1;
  mix-blend-mode: difference;
  color: white;
}

.image-button:hover + span {
  font-size: 32vh;
  opacity: 1;
}
<div class="button-container">
  <button class="image-button">
    <img src="https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif" alt="Button Image">
  </button>
  <span>text1</span>
  <button class="image-button">
    <img src="https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif" alt="Button Image">
  </button>
  <span>text2</span>
  <button class="image-button">
    <img src="https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif" alt="Button Image">
  </button>
  <span>text3</span>
</div>

As you can see is not very clean, it glitch a bit, and the text grow from the center of the screen and not from the center of the button.

What should I change?

2

Answers


  1. Here are updated code, check it and leave me feedback so that I can fix it.

    body {
      background-color: white;
    }
    
    .button-container {
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    
    .image-button {
      position: relative;
      width: 10vw;
      height: 10vw;
      background: none;
      border: none;
      transition: all 0.5s ease;
    }
    
    .image-button img {
      width: 100%;
      height: 100%;
      transition: all 0.5s ease;
    }
    
    .image-button:hover img {
      transform: scale(0.6); 
    }
    
    .button-text {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%) scale(0.1);
      font-size: 25vh;
      transition: all 0.3s ease;
      opacity: 0;
      z-index: 1;
      mix-blend-mode: difference;
      color: white;
      pointer-events: none; /* Added to allow hover effect on buttons */
    }
    
    .image-button:hover .button-text {
      transform: translate(-50%, -50%) scale(1);
      opacity: 1;
    }
    <div class="button-container">
      <button class="image-button">
        <img src="https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif" alt="Button Image">
        <span class="button-text">text1</span>
      </button>
      <button class="image-button">
        <img src="https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif" alt="Button Image">
        <span class="button-text">text2</span>
      </button>
      <button class="image-button">
        <img src="https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif" alt="Button Image">
        <span class="button-text">text3</span>
      </button>
    </div>
    Login or Signup to reply.
  2.     body {
            background-color: white;
          }
          
          .button-container {
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            gap: 10px;
          }
          
          .image-button {
            position: relative;
         
            width: 10vw;
            height: 10vw;
            background: none;
            border: none;
            background-image: url('https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif');
        transition: all 0.3s ease;
        }
          
         
         .image-button span {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 1;
            mix-blend-mode: difference;
            color: red;
            opacity: 1;
            transition: font-size 0.3s ease;
        
           
          }
           .image-button:hover {
            transform: scale(0.6); 
            
          }
          
          .image-button:hover span {    
            font-size: 16vh;
            opacity: 1;
          }
    
    
    
    
    
        <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width='device-width', initial-scale=1.0">
        <title>Document</title>
        <link href="style.css" rel="stylesheet"/>
     
    </head>
    <body>
        <div class="button-container">
            <button class="image-button">
              <!-- <img src="https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif" alt="Button Image"> -->
              <span>text1</span>
            </button>
       
            <button class="image-button">
              <!-- <img src="https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif" alt="Button Image"> -->
              <span>text2</span>
            </button>
    
            <button class="image-button">
              <!-- <img src="https://images.squarespace-cdn.com/content/v1/55fc0004e4b069a519961e2d/1442590746571-RPGKIXWGOO671REUNMCB/image-asset.gif" alt="Button Image"> -->
              <span>text3</span>
            </button>
           
            
          </div>
    </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search