Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center text with letter-spacing on hover only css [duplicate]

i try something when i hover my button. Currenttly that's my code

.btn {
  border:solid 1px purple;
  color:purple;
  background: white;
  padding:10px 15px;
  text-align: center;
  transition: .5s;
}

.btn:hover{
  letter-spacing: 1px;
}
<button class="btn">My bouton</button>

The problem is, when i hover my button, the text expand, the button expand too on the right. I want to make button not expand (keep original size), only text inside expand and centerized, without put a width size, because i want it as component (I would never know the size of the text.) ALL in css only.

The only one way i've find is to add a data attribute who come over, but i don't like this way because i reapt my text 2 times.

For you it's possible only CSS ? if yes how ? Thanks a lot

like image 689
countrystores Avatar asked Nov 30 '25 17:11

countrystores


1 Answers

Hide the original button text and use it inside the after/before using below CSS rules.

.btn {
  border: solid 1px purple;
  color: white;
  background: white;
  padding: 10px 15px;
  text-align: center;
  position: relative;
}

.btn:hover::after{
  letter-spacing: 1px;
}

.btn::after{
  content: 'My Button';
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  color: purple;
  transition: 0.5s;
}
<button class="btn">My bouton</button>
like image 96
Alisha Avatar answered Dec 02 '25 08:12

Alisha