user6845055
user6845055

Reputation:

Fix on hover expands element

I don't know how to solve this problem: on hover of 'a' elements the div enlarges and the other 'a' move.

Thanks for help

https://jsfiddle.net/0r2v2qyp/

Here html:

<a id="btn_agency" class="btn_presentation">Agenzia</a>
<a id="btn_student" class="btn_presentation">Studente</a>
<a id="btn_prof" class="btn_presentation">Docente</a>
<a id="btn_admin" class="btn_presentation">Segreteria</a>

And css:

    .btn_presentation {
    float: left;
    margin: 10px;
    color: royalblue;
    font-size: 20px;
    position: relative;
    letter-spacing: 0;
    text-transform: uppercase;
    transition: all .2s ease-out;
}

.btn_presentation:after {
    content: '';
    position: relative;
    display: block;
    margin: 0 auto;
    width: 0;
    height: 0;
    background: red;
    transition: all .2s ease-out;
}

.btn_presentation:hover {
    letter-spacing: 5px;
    transition: all .3s ease-in;
}

.btn_presentation:hover:after {
    width: 100%;
    height: 2px;
    transition: all .2s ease-in-out;
}

Upvotes: 0

Views: 67

Answers (1)

Asons
Asons

Reputation: 87191

To be able to animate the letter-spacing, you have to give the div a fixed width wide enough to make up for the text when expanded.

div {
  display: inline-block;
  margin: 10px;
  width: 120px;
}

.btn_presentation {
  display: inline-block;
  color: royalblue;
  font-size: 20px;
  position: relative;
  letter-spacing: 0;
  text-transform: uppercase;
  transition: all .2s ease-out;
}

.btn_presentation:after {
  content: '';
  position: relative;
  display: block;
  margin: 0 auto;
  width: 0;
  height: 0;
  background: red;
  transition: all .2s ease-out;
}

.btn_presentation:hover {
  letter-spacing: 5px;
  transition: all .3s ease-in;
}

.btn_presentation:hover:after {
  width: 100%;
  height: 2px;
  transition: all .2s ease-in-out;
}
<div>
  <a id="btn_agency" class="btn_presentation">Agenzia</a>
</div>
<div>
  <a id="btn_student" class="btn_presentation">Studente</a>
</div>
<div>
  <a id="btn_prof" class="btn_presentation">Docente</a>
</div>
<div>
  <a id="btn_admin" class="btn_presentation">Segreteria</a>
</div>


An option could instead be to scale it using transform

div {
  display: inline-block;
  margin: 10px;
}

.btn_presentation {
  display: inline-block;
  color: royalblue;
  font-size: 20px;
  position: relative;
  letter-spacing: 0;
  text-transform: uppercase;
  transition: transform .2s ease-out;
}

.btn_presentation:after {
  content: '';
  position: relative;
  display: block;
  margin: 0 auto;
  width: 0;
  height: 2px;
  background: red;
  transition: width .2s ease-out;
}

.btn_presentation:hover {
  transform: scale(1.2);
  transition: transform .3s ease-in;
}

.btn_presentation:hover:after {
  width: 100%;
  transition: width .2s ease-in-out;
}
<div>
  <a id="btn_agency" class="btn_presentation">Agenzia</a>
</div>
<div>
  <a id="btn_student" class="btn_presentation">Studente</a>
</div>
<div>
  <a id="btn_prof" class="btn_presentation">Docente</a>
</div>
<div>
  <a id="btn_admin" class="btn_presentation">Segreteria</a>
</div>


Updated based on comment

This stretch the word only side ways

div {
  display: inline-block;
  margin: 10px;
}

.btn_presentation {
  display: inline-block;
  color: royalblue;
  font-size: 20px;
  position: relative;
  letter-spacing: 0;
  text-transform: uppercase;
  transition: transform .2s ease-out;
}

.btn_presentation:after {
  content: '';
  position: relative;
  display: block;
  margin: 0 auto;
  width: 0;
  height: 2px;
  background: red;
  transition: width .2s ease-out;
}

.btn_presentation:hover {
  transform: scaleX(1.2);
  transition: transform .3s ease-in;
}

.btn_presentation:hover:after {
  width: 100%;
  transition: width .2s ease-in-out;
}
<div>
  <a id="btn_agency" class="btn_presentation">Agenzia</a>
</div>
<div>
  <a id="btn_student" class="btn_presentation">Studente</a>
</div>
<div>
  <a id="btn_prof" class="btn_presentation">Docente</a>
</div>
<div>
  <a id="btn_admin" class="btn_presentation">Segreteria</a>
</div>

Upvotes: 1

Related Questions