LoginSignup
2
1

More than 5 years have passed since last update.

cssで下線アニメ―ション時、IEで下線が親要素をはみ出す

Posted at

要素にマウスオーバーした際に、下線がビョ~ンてなるアニメーションを、センターから広がるように実装してたのだけど、
IEの時にアニメーション中に
下線が親要素を突き抜けてから親要素の100%幅に収まる、
という謎挙動をしてしまった

問題のおきたコード

a{
  display:inline-block;
  position:relative;
  height:50px;

  &:after{
  content: "";
  width: 0; /* 初めはゼロ */
  height: 2px; /*2pxの高さの*/
  background-color: #f00; /*赤背景*/
  position: absolute;
  bottom: -2px;
  left: 50%; /*relative要素を起点に左に50%*/
  transition: .3s;
  transform: translate(-50%, 0); /*要素を左に半分ズラす*/

  &:hover{
    &:after{
      width:100%; /* 0.3sかけて100%になる */
    }
  }
}

:after疑似要素を border使わず、中央揃えも position:absoluteしてtransform を使うなど、複合的な要因でIEで起きる例かもしれません。
margin:0 auto で中央揃えというのもあるのですが、
bottom の位置を調整したかったので上記の実装になりました。

解決

謎挙動を解決するには、
width: 0 → width: 100%
width: 0% → width: 100%

と単位を揃えることで解決しました。
scaleなどを使って拡大したほうが確実かもしれません

参考:CSS で underline に animation をつける

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1