How to automatically change the text size inside a div?
Solution 1
I understand your question this way, you would like to fit some text into a given div with a fixed dimension, and if the div is too small to show all the text, the font size should be shrinked until the text fits into the div. If that's the point, here is my solution.
Here is an example with a jQuery implementaion: http://jsfiddle.net/MYSVL/2/
Here is a div
<div id="fitin">
<div>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</div>
</div>
With a fixed size
#fitin {
width: 300px;
height: 100px;
border: 1px solid black;
overflow: hidden;
font-size: 1em;
}
This JavaScript will do the job.
$(function() {
while( $('#fitin div').height() > $('#fitin').height() ) {
$('#fitin div').css('font-size', (parseInt($('#fitin div').css('font-size')) - 1) + "px" );
}
});
Solution 2
FlowType.js will do just that: resize the font to match the bounding element, be it a div or another element type.
An example of implementing FlowType:
-
Set up your style
body { font-size: 18px; line-height: 26px; }
-
Download FlowType from GitHub and include a reference to it in your head element
flowtype.jQuery.js
-
Call FlowType
$('body').flowtype();
-
(optional) Update default settings
$('body').flowtype({ minimum : 500, maximum : 1200, minFont : 12, maxFont : 40, fontRatio : 30, lineRatio : 1.45 });
Check out their homepage for an interactive demo
Solution 3
The question has been answered since long, but I'd like to add some notes.
Since, an extra jQuery plugin has been added to the universe: FlowType.js
https://github.com/simplefocus/FlowType.JS
I tried my hand at the given solutions and plugins (including FlowType.JS), in the end I wrote my own. I just like to include it here as 'inspiration'. I use a different approach: I calculate the ratio that the size of the text is larger then it's parent. I don't know if it makes sense, but for me it works nicely.
/* shrinkText jQuery Plugin */
(function( $ ){
$.fn.shrinkText = function() {
var $_me = this;
var $_parent = $_me.parent();
var int_my_width = $_me.width();
var int_parent_width = $_parent.width();
if ( int_my_width > int_parent_width ){
rl_ratio = int_parent_width / int_my_width;
var int_my_fontSize = $_me.css("font-size").replace(/[^-\d\.]/g, '');
int_my_fontSize = Math.floor(int_my_fontSize * rl_ratio);
$_me.css("font-size", int_my_fontSize + "px");
}
};
})( jQuery );
It assumes a inline-element within a block-level element, it shrinks the inline element by recalculating the font-size.
HTML:
<h1 style="white-space:nowrap;">
<a href="#" >Macrobenthos of the North Sea - Miscellaneous worms</a>
</h1>
JavaScript:
if( jQuery().shrinkText ){
$("#title a").shrinkText();
}
Solution 4
I'm not completely sure I understand your question but I think what you're asking is how to fit text in a particular container of predefined size. I'll try to answer this question.
Client side
When you want to do this on demand on the client you will have to run Javascript. The idea is to:
generate an invisible
div
and set its style to:position: absolute; top: 0; left: 0; width: auto; height: auto; display: block; visibility: hidden; font-family: /* as per your original DIV */ font-size: /* as per your original DIV */ white-space: /* as per your original DIV */
copy your text to it
check whether DIV's width exceeded the size of your original DIV.
adjust
font-size
value by not simply incrementing/decrementing but rather by calculating width difference between your invisible and original DIV. This will deviate to correct size much faster.go to step 3.
Server side
There is no reliable way of setting font size on the server so it will fit your client rendered container. Unfortunately you can only approximate sizes on pretested empirical data.
Related videos on Youtube
Comments
-
useCase almost 2 years
I have a background image with a
div
. I want to write some text, but this text should change the font size through thediv
.-
Vincent Ramdhanie almost 13 yearsCan you clarify what you mean by "fixed automatically"
-
Robert Koritnik almost 13 yearsAre you asking how to resize text so it fits your DIV size?
-
-
Jaider about 12 yearsIf you add this
while( $('#fitin').height() > $('#fitin div').height() ) { $('#fitin div').css('font-size', (parseInt($('#fitin div').css('font-size')) + 1) + "px" ); }
Then you will be able to increment the font-size as well -
Erma Isabel almost 11 years@Web_Designer That works awesome. But text wont be having small font size if i am refreshing the window after re-sizing. Is there any alternative for that? Thank you