Search code examples
htmlcss

How do I make a text grow from the center of a button?


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?


Solution

  • 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>