There have been similar questions asked, but the solutions do mesh with what I'm trying to do. Basically, I have an article with a title (<h1>
). I don't want to control the length of the title, but I also don't want the title to appear on multiple lines. Is there a way with css or jQuery to resize text based on the width of a <div>
tag?
I know what I would do with pseudocode if I could detect the overlap of the text to the edge of the <div>
:
var fontize = $("#title").css("font-size");
var i = /*remove unit from integer*/
while( /*text overlaps div*/ ){
$("#title").css("font-size", --i+"pt");
}
If there's a CSS attribute I can set that would be even nicer, but I can't seem to find one (overflow wouldn't work in this situation).
I've make a very little jQuery stuff to adjust the font size of your div. It even works with multiple lines, but will only increase font's size (you can simply set a default font size of 1px if you want). I increases the font size until having a line break, set decreases it 1px. No other fancy stuff, hidden div or so. Simply add the
quickfit
class to your div.@Clovis Six thank for your answer. It prove very usefull to me. A pity I cannot thanks you more than just a vote up.
Note: I have to change the "$J(" for "$(" for it to work on my config.
evendo, this out of the scope of this question and not in the use of SO, I extended your code to make it work for multi-line box with max-height.
PS: I know this should be a comment, but comments don't let me post code properly.
I wasn't in love with the other solutions out there, so, here's another one. It requires the tag you're resizing the text inside of:
Be fixed height
Not be so long that it'd overrun the boundaries at 10px - the idea being, you don't want it to shoot below that and resize text to become unreadable. Not an issue in our use case, but if it's possible in yours you'd want to give some thoughts to separately truncating before running this.
It uses a binary search to find the best size so I suspect it outperforms a lot of the other solutions here and elsewhere that just step the font-size down by a pixel over and over. Most browsers today support decimals in font sizes as well, and this script has some benefits there since it will get to within .1px of the best answer, whatever that is, even if it's a relatively long decimal. You could easily change the
max - fontSize < .1
to some other value than.1
to get less precision for less CPU usage.Usage:
Code:
http://jsfiddle.net/b9chris/et6N6/1/
Here are 3 functions I use frequently to get the text width, height and adjust the size to the container's width.
I had a similar issue, which made me write my own plugin for this. Have a look at jquery-quickfit (which is quite similar to Robert Koritnik's solution, which I really like).
In order to prevent the headline to span multiple lines, just add a css style of:
to the element.
After including jquery and quickfit in the header. You can trigger quickfit with:
It meassures and calculates a size invariant meassure for each letter of the text to fit and uses this to calculate the next best font-size which fits the text into the container.
The calculations are cached, which makes it very fast when dealing having to fit multiple text or having to fit a text multiple times, like e.g., on window resize (there is almost no performance penalty on resize).
Demo for a similar situation as yours
Further documentation, examples and source code are on the project page.
Today I created a jQuery plugin called jQuery Responsive Headlines that does exactly what you want: it adjusts the font size of a headline to fit inside its containing element (a div, the body or whatever). There are some nice options that you can set to customize the behavior of the plugin and I have also taken the precaution to add support for Ben Altmans Throttle/Debounce functionality to increase performance and ease the load on the browser/computer when the user resizes the window. In it's most simple use case the code would look like this:
...which would turn all h1 on the page elements into responsive one-line headlines that adjust their sizes to the parent/container element.
You'll find the source code and a longer description of the plugin on this GitHub page.
Good luck!