JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://code.jquery.com/jquery-3.4.0.js"></script>
<div>Reallylongword test</div>
CSS
div {
max-width: 120px;
width: auto;
display: inline-block;
border: 1px solid black;
padding: 0.2rem;
}
JavaScript
$.fn.textWidth = function(){
var html_org = $(this).html();
var html_calc = '<span>' + html_org + '</span>';
$(this).html(html_calc);
var width = $(this).find('span:first').width();
$(this).html(html_org);
return width;
};
var textWidth = $("div").textWidth();
$("div").width(textWidth);