I do not think that dynamically changing the width of an element depending on its content is possible, since there is no transition for the content. The content of the element changes instantly, and the width does not get a numerical value in your case - but it is customizable.
A solution that can sometimes be applied: using a function to roughly calculate the width of the text so that you can set the numerical width for your element when changing . >.
Here is a simple one I made.
function getTextWidth(text, css) { var e = $('<span></span>'); // dummy element e.css(css); // set properties e.text(text); // set test var width = e.appendTo($('body')).width(); // append and get width e.remove(); // remove from DOM return width; }
Compare the usage example .
source share