var logging = false, l = function( log ){if (logging == true){console.log(log)}}; function textResize( elem ) { var child = $( elem ).children(), parent = $( elem ), runs; //Setup the enlarge and contract functions to be used later on if needed function enlarge() { if (child.height() < parent.height()) { child.css("font-size", parseInt(child.css("font-size").replace("px",""))+1+"px"); l("enlarging child"); enlarge(); } else if (child.height() > parent.height()) { contract(); } }; function contract() { if (child.height() > parent.height()) { child.css("font-size", parseInt(child.css("font-size").replace("px",""))-1+"px"); l("contracting child"); contract(); } }; //decide if we need to enlarge or contract the child element if (runs === undefined) { if(child.height() < parent.height()) { //Enlarge enlarge(); } else if (child.height() > parent.height()) { //Contract contract(); } } } textResize("#container"); $( window ).resize(function(){ textResize("#container"); });
<div id="container"> <p>This is some text that will be large and need to be shrunken down to be usable here</p> </div> <div id="output"></div>
* { padding: 0; margin: 0; } body { font-size: 46px; font-family: Helvetica, Arial, Sans-Serif; } div { width: 80%; margin: 0 auto; } #container { background: #fff; color: #333; padding: 20px; height: 130px; } #container p { /*transition: font-size 0.1s ease;*/ } #output { font-family: serif; font-size: 18px; }