Why does this happen?
When you set width
to initial
, you're really setting the width
to auto
(which is the initial value of the width
property).
This issue dates as far back as 2007, when this bug was reported on WebKit. You can follow the conversation on that report and see:
The CSS WG has resolved that transitions don't run to/from auto values.
This is the behavior for all of the major browsers, so when trying to animate width
to auto
, we're out of luck and we have to go with an existing workaround.
The Workarounds
There's a few workarounds for animating to auto
, but animating max-width
is by far the most popular as it was already referenced by the two answers before this.
Rather than listing and explaining the workarounds in this post, I suggest you see this awesome article on the workarounds for this problem.
Summarizing the article, the workarounds are:
- Animating
max-height
with explicit values
- Animate
transform
with scaleX
- Use JavaScript to achieve the desired effect
- Use Flexbox
If you really need the functionality of animating to auto
, your best bet is animating with JavaScript.