Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically changing the size of font size based on text length using css and html

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.

like image 964
user2613399 Avatar asked Aug 14 '13 10:08

user2613399


People also ask

How do I automatically adjust font size in CSS?

Syntax: font-size-adjust: number|none|initial|inherit; Below are the examples that illustrates the use of font-size-adjust property.

Which CSS property changes the size of the text?

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.

How do I change font size in responsive design?

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.

Which is the best option to adjust the font size so that we make the best use of the screen in CSS?

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.


1 Answers

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>
like image 180
putvande Avatar answered Sep 21 '22 15:09

putvande