views:

535

answers:

1

Let's say you've got a simple fixed-width layout that pulls a title from a MySQL database.

CSS:

 #wrapper {
    width: 800px;
 }

 h1 {
    width: 100%;
 }

HTML:

 <html>
  <body>
   <div id="wrapper">
    <h1> $titleString </h1>
   </div>
  </body>
 </html>

But the catch is, the length of the title string pulled from your MySQL database varies wildly. Sometimes it might be 10 characters, sometimes it might be 80. It's possible to establish a min & max character count.

How, if at all possible, do I get the text-size of my <h1>$titleString</h1> to enlarge/decrease on-the-fly such that the string is only ever on one line and best fit to that line length? I've seen a lot of questions about resizing the div - but in my case the div must always be 100% (800px) and I want to best-fit the title.

Obviously a maximum text-size value would have to be set so 5 character strings don't become gargantuan.

Does anyone have a suggestion? I'm only using PHP/MySQL/CSS on this page at the moment, but incorporation of another language is fine if it means I can solve the problem.

The only thing I can think of is a bruteforce approach whereby through trial and error I establish acceptable string character count ranges matched with CSS em sizes, but that'd be a pretty ugly implementation from the code side.

+1  A: 

Maybe one solution would be to use javascript to calculate the width of the given text in some standard font size, then resize until you get a reasonable width. In Javascript you can calculate the width of the text like this:

 var title = document.getElementById("title");
 var width = title.offsetWidth;

Initailly the text is placed in a span with no styles applied apart from the font-size.

 <span id="title">The Title</span>

You can then check if the width of the text is greater than 800px and reduce the font-size until the width is less than or equal to. It is important that the title span does not have other styles applied to it initially or the results will be wrong.

You can also check if the width is too short, maybe less than 200px and increase the font-size accordingly.

Once you have satisfied yourself that the text is sized appropriately then move the text into the h1 and set the font-size.

This question describes calculating text width.

For example, I quickly wrote this using jQuery. Suppose you have your HTML like this:

<span id="mytitle" style="font-size:14pt;">
    A very long title that may be more than 800px in width. And even longer.
</span>

I used a span because it resizes to the width of the text rather than the container. And the jQuery code:

 $(document).ready(function(){        
    var w = $("#mytitle").width();
    while(w > 800){
       var currentFontSize = $('#mytitle').css('font-size');
       var currentFontSizeNum = parseFloat(currentFontSize, 10);
       var newFontSize = currentFontSizeNum*0.95;
       $('#mytitle').css('font-size', newFontSize);
       w = $("#mytitle").width();
   //alert("Width:" + w);
}
  });

That is, repeatedly reduce the font-size by 5% until the total width is less than or equal to 800px.

Vincent Ramdhanie
thanks for the great detailed example, I should be able to get it working! :-)
Andrew Heath