CSS :hover transform scale command rotate object instead

946 views Asked by At

.arrow {
  display: inline-block;
  height: 8px;
  width: 8px;
  margin-top: 20px;
  border-top: solid black 2.5px;
  border-left: solid black 2.5px;
  -ms-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
  -webkit-transition: transform 1s;
  -moz-transition: transform 1s;
  transition: transform 1s;
  cursor: pointer;
}
.arrow::before {
  content: "";
  display: inline-block;
  width: 18px;
  margin-left: -3px;
  margin-bottom: 7px;
  border-top: solid black 1.8px;
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}
.arrow:hover {
  -ms-transform: scale(1.2);
  -webkit-transform: scale(1.2);
  -moz-transform: scale(1.2);
  transform: scale(1.2);
}
<span class="arrow"></span>

I draw an arrow using css and try to create a hover scale up effect by using css :hover and transform:scale. However the codes rotate the arrow first and then scale it up later. I am wondering why is rotating happening and how do I fix it? Thanks in advance!

3

There are 3 answers

0
maioman On BEST ANSWER

.arrow {
  display: inline-block;
  height: 8px;
  width: 8px;
  margin-top: 20px;
  border-top: solid black 2.5px;
  border-left: solid black 2.5px;
  -ms-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
  -webkit-transition: transform 1s;
  -moz-transition: transform 1s;
  transition: transform 1s;
  cursor: pointer;
}
.arrow::before {
  content: "";
  display: inline-block;
  width: 18px;
  margin-left: -3px;
  margin-bottom: 7px;
  border-top: solid black 1.8px;
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}
.arrow:hover {
  -ms-transform: rotate(-45deg) scale(1.2);
  -webkit-transform: rotate(-45deg) scale(1.2);
  -moz-transform: rotate(-45deg) scale(1.2);
  transform: rotate(-45deg) scale(1.2);
}
<span class="arrow"></span>

the problem is that transform properties don't sum up, if you override a property and want to keep it's original value you need to repeat it.

0
HelloWorld On

Use this CSS for hover:

.arrow:hover {
  -ms-transform: scale(1.2) rotate(-45deg);
  -webkit-transform: scale(1.2) rotate(-45deg);
  -moz-transform: scale(1.2) rotate(-45deg);
  transform: scale(1.2) rotate(-45deg);
}

Rotate was set on the entire element and was not accounted for during the hover transform.

here is a working example

0
Felix A J On

You can give transform only to :before so it won't rotate on hover.

.arrow {
  display: inline-block;
  height: 1px;
  width: 18px;
  border-top: solid black 1.8px;
  -webkit-transition: transform 1s;
  -moz-transition: transform 1s;
  transition: transform 1s;
  cursor: pointer;
}
.arrow:before {
  content: "";
  display: inline-block;
  margin-top: -10px;
  height: 8px;
  width: 8px;
  border-top: solid black 2.5px;
  border-left: solid black 2.5px;
  -ms-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
  -webkit-transition: transform 1s;
  -moz-transition: transform 1s;
  transition: transform 1s;
  cursor: pointer;
position: relative;
top: -9px;
}
.arrow:hover {
  -ms-transform: scale(1.2);
  -webkit-transform: scale(1.2);
  -moz-transform: scale(1.2);
  transform: scale(1.2);
}
<span class="arrow"></span>