css3 从宽度自动过渡到“n”像素

Aje*_*jey 0 css css-transitions

如何将 css 过渡应用于初始宽度设置为的 div auto

但是,如果我将初始宽度设置为某个数字,例如 50px,过渡就会开始。

这是 -演示

.container {
  width: 200px;
  background: red;
  height: 50px;
}


.child1 {
  background: black;
  display: inline-block;

}

.child2 {
  background: blue;
    display: inline-block;
  width: auto;  /* this does not work */

    /* width: 50px; */ /* this works */
  float: right;
  -webkit-transition: width 2s;
      transition: width 2s;
}

.child2:hover {
  width: 100px;
}
Run Code Online (Sandbox Code Playgroud)

Vit*_*des 5

默认情况下min-width将采用内部元素width

.container {
  width: 200px;
  background: red;
  height: 50px;
}
.child1 {
  background: black;
  display: inline-block;
}
.child2 {
  background: blue;
  display: inline-block;
  width: auto;
  min-width: 0px; /*changed width to min-width */
  float: right;
  -webkit-transition: 2s;
  transition: 2s;
}
.child2:hover {
  min-width: 100px; /* changed from width to min-width */
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="child1">
    child1
  </div>
  <div class="child2">
    child2
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)