I’m looking for a way to autoresize the text inside a div to its maximum size. The maximum size is the biggest size possible while still fitting the div.
I know it’s not possible to do this in CSS. Is it possible with the use of javascript?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use such trick in pure JavaScript to “auto resize” the font size:
Live test case.
Start with the actual font size of the
<div>if you know it, otherwise better use smaller number as the first number.The above code works on the principle that when the text is bigger than the set width of its container, the
scrollWidthproperty which is the “actual” width will be bigger than the set width.Edit: Firefox won’t update the
scrollWidthproperty if theoverflowCSS property is set to its default “visible” value, so you must set it to “auto” – the code is doing it for you, but also possible setting it via CSS if you prefer.