Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.image-button {
  position: relative;
  overflow: hidden;
  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-container span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.1);
  font-size: 12vh;
  transition: all 0.3s ease;
  opacity: 0;
  z-index: 1;
  mix-blend-mode: difference;
  color: white;
}

.image-button:hover span {
  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>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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading