0

I need to animate a div from a width of 0 (initial) to a width fit-content (a width that is the same as the width of its content)

$('div').css('width', 'fit-content');

DEMO

However, if I do this the animation

transition: width .5s ease-in-out;

doesn't work anymore. So the question is how can I animate just to fit the content ?

UPDATE: I found the solution here (DEMO

Community
  • 1
  • 1
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

1 Answers1

0

You can do this:

setTimeout(function () {
    $('div').css('width', $('div').find('span').width() + 'px');
}, 1000);

like this JSfiddle: JSfiddle

Tariq
  • 2,853
  • 3
  • 22
  • 29
  • There are a few ways this could be enhanced, like switching to the actual fit-content value once the animation completes, or restarting the animation if something in the window is resized. Those might not always be necessary, though. – Katana314 Jan 20 '16 at 19:13