I need to display user entered text into a fixed size div. What i want is for the font size to be automatically adjusted so that the text fills the box as much as possible.
I'd probably want to start with a maximum font size and while the text is too big to fit the container, shrink the font size until it fits and the font must be displayed as a single line. Is it possible to do this using only css and html.
Syntax: font-size-adjust: number|none|initial|inherit; Below are the examples that illustrates the use of font-size-adjust property.
The font-size CSS property sets the size of the font. Changing the font size also updates the sizes of the font size-relative <length> units, such as em , ex , and so forth.
To make font size responsive in steps, set the base font size at each breakpoint. To create fluid text that smoothly changes size, use the calc() function to calculate a ratio between pixels and viewport widths, this will make the base font size grow at the correct rate relative to the screen width.
The font-size-adjust property gives you better control of the font size when the first selected font is not available. When a font is not available, the browser uses the second specified font. This could result in a big change for the font size. To prevent this, use the font-size-adjust property.
Say you have this:
<div id="outer" style="width:200px; height:20px; border:1px solid red;"> <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mollis dui felis, vel vehicula tortor cursus nec</div> </div>
Then you can do something like:
$(document).ready(function () { resize_to_fit(); }); function resize_to_fit(){ var fontsize = $('div#outer div').css('font-size'); $('div#outer div').css('fontSize', parseFloat(fontsize) - 1); if($('div#outer div').height() >= $('div#outer').height()){ resize_to_fit(); } }
Working Sample
$(document).ready(function() { resize_to_fit(); }); function resize_to_fit() { var fontsize = $('div#outer div').css('font-size'); $('div#outer div').css('fontSize', parseFloat(fontsize) - 1); if ($('div#outer div').height() >= $('div#outer').height()) { resize_to_fit(); } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="outer" style="width:200px; height:20px; border:1px solid red;"> <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mollis dui felis, vel vehicula tortor cursus nec</div> </div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With