1

I am trying to adjust text size so it can fill a certain space

For example : so, lets say that my HTML is like this :

<h1 style="width:120px;">some text</h1>

I want the word "some text" to fill the 120px automatically knowing that "some text" could be anything! I do not care if the font width is stretched or some spaces has been added to the word to make it fit the 120px width.

I know that in CSS we have overflow:hidden; and overflow:scroll; but those two will not help me with anything

overflow:hidden; will hide any extra characters outside the 120px overflow:scroll; will make a scroll which I do not want !

any solutions? maybe a Jquery plugin or something? I have looked everywhere but I could not find anything.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
syrkull
  • 2,295
  • 4
  • 35
  • 68

2 Answers2

2

You may adapt the font to the available space using such a script :

var fontSize = 100;
var reduce = function() {
    $('#txt').css('font-size', fontSize);
    $('#mes').html(fontSize);
    if ($('#txt').width()>MAX_MIDTH) {
       fontSize -= 1;
        reduce();
    }
};
reduce();
$('#width, #height').change(function(){fontSize = 100;reduce()});

Demonstration

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Yes, for [a question a little similar](http://stackoverflow.com/questions/13416673/javascript-convert-width-and-height-to-font-size) (I'm trying to see if this one can be said to be a duplicate). – Denys Séguret Nov 25 '12 at 20:57
1

Align your text using text-align: justify

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278