Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically change the text size inside a div?

I have a background image with a div. I want to write some text, but this text should change the font size through the div.

like image 884
useCase Avatar asked May 24 '11 15:05

useCase


3 Answers

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" );     } }); 
like image 171
DanielB Avatar answered Oct 09 '22 11:10

DanielB


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:

  1. Set up your style

    body { font-size: 18px; line-height: 26px; } 
  2. Download FlowType from GitHub and include a reference to it in your head element

    flowtype.jQuery.js 
  3. Call FlowType

    $('body').flowtype(); 
  4. (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

like image 25
jdhurst Avatar answered Oct 09 '22 12:10

jdhurst


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:

  1. 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 */
    
  2. copy your text to it

  3. check whether DIV's width exceeded the size of your original DIV.

  4. 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.

  5. 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.

like image 41
Robert Koritnik Avatar answered Oct 09 '22 10:10

Robert Koritnik