CSS transition auto width

前端 未结 1 783
一个人的身影
一个人的身影 2020-11-28 12:45

I have an element whose width I\'d like to animate when its contents change. It has width: auto, and this never changes. I\'ve seen this trick, but that\'s for

相关标签:
1条回答
  • 2020-11-28 13:15

    As I commented, one can't animate auto (yet), so either use the max-width/max-height trick, or, if you need it to be more exact, set the width using a script.

    With the max-width/max-height trick, give it a value big enough to accommodate the widest.

    Stack snippet

    .myspan {
      display: inline-block;
      font-size: 30px;
      background-color: #ffffd;
      vertical-align: bottom;
    }
    .myspan::after {
      content: " \00a0\f12a ";
      font-family: ionicons;
      font-size: 80%;  
      display: inline-block;
      max-width: 0;
      transition: max-width .6s;
      vertical-align: bottom;
      overflow: hidden;
    }
    .myspan:hover::after {
      max-width: 80px;
      transition: max-width 1s;
    }
    <link href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet" />
    
    <span class="myspan">Hello!</span>

    0 讨论(0)
提交回复
热议问题