Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1075fb

Browse files
committed
Font-size agnostic. Added min- & max- font-size options.
1 parent 9cb6107 commit a1075fb

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

README.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
11
# FitText.js, a jQuery plugin for inflating web type
2-
FitText makes font-sizes flexible. Use this plugin on your fluid or responsive layout to achieve scalable headlines that fill the width of a parent element.
2+
FitText makes font-sizes flexible. Use this plugin on your fluid or responsive layout to achieve scalable headlines that fill the width of the parent element.
33

44
## How it works
5-
If you're working on a responsive design, take whatever headline you'd like to scale and set the item to FitText. Oh. and you'll want to include jQuery n' all that too.
5+
Here is a simple FitText setup:
66

77
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
88
<script src="jquery.fittext.js"></script>
99
<script>
1010
$("#responsive_headline").fitText();
1111
</script>
1212

13-
[Pretty Cool](http://www.hulu.com/watch/194733/saturday-night-live-miley-cyrus-show). Your text will now resize based on the width of the item. (by default: ~1/10th of the element's width).
13+
[Pretty Cool](http://www.hulu.com/watch/194733/saturday-night-live-miley-cyrus-show). Your text should now resize based on the width of the parent element. By default: *Font-size = 1/10th of the parent element's width*.
1414

1515
### The Compressor
16-
The default setting works pretty well, but when it doesn't FitText has one setting you can adjust. If your text resizes poorly or is resizing all hurdy gurdy, you'll want to turn tweak up/down the compressor. It works a little like a guitar amp.
16+
If your text is resizing poorly, you'll want to turn tweak up/down "The Compressor". It works a little like a guitar amp. The default is `1`.
1717

18-
$("#responsive_headline").fitText(1.2); // turn the compressor up (font will shrink a bit more aggressively)
19-
$("#responsive_headline").fitText(0.8); // turn the compressor down (font will shrink less aggressively)
18+
$("#responsive_headline").fitText(1.2); // Turn the compressor up (text shrinks more aggressively)
19+
$("#responsive_headline").fitText(0.8); // Turn the compressor down (text shrinks less aggressively)
2020

2121
This will hopefully give you a level of "control" that might not be pixel perfect, but scales smoothly & nicely.
2222

23-
## 3-Step Setup
23+
### _new:_ minFontSize & maxFontSize
24+
FitText now allows you to specify two optional values: `minFontSize` and `maxFontSize`. Great for situations when you want responsive text but also want to preserve hierarchy.
2425

25-
* Add all the Javascripts (jQuery, FitText and `$(element).fitText();` block) as described above.
26-
* Set your `font-size` in your CSS. This will make act like a max-font-size.
27-
* Squeeze your browser.
26+
$("#responsive_headline").fitText(1.2, { minFontSize: 20, maxFontSize: '80px' })
27+
28+
The options accept either integers or `px` values.
2829

2930
## CSS Tips
3031

31-
* Set your target headline to `width: 100%` in your CSS.
32-
* Set a font-size, this will act like a `max-font-size`.
32+
* Make sure your headline is `display: block;` or inline-block with a specified width `display: inline-block; width: 100%`.
3333
* Be ready to tweak till everything balances out.
34-
* So far, FitText seems to work with other fun properties like text-shadow
35-
* It also works with [Lettering.js #synergy](http://github.com/davatron5000/Lettering.js)!
3634

3735
## Disclaimers
3836
This is the part of the show where we cover our butts.
3937

4038
### Intended for Fluid Width Designs
41-
We built this to satisfy a need for fluid resizing text on responsive designs. Mostly for use on [Trent Walton's blog](http://trentwalton.com), which he's using it all over. If you're not going fluid and/or want exact fitting text, we recommend checking out [BigText](https://github.com/zachleat/BigText) by Zach Leatherman.
39+
We built this to satisfy a need for fluid resizing text on responsive designs. Mostly for use on [Trent Walton's blog](http://trentwalton.com), which he's using it all over. If you want more exact fitting text, we recommend checking out [BigText](https://github.com/zachleat/BigText) by Zach Leatherman.
4240

4341
### window.resize() tsk tsk tsk...
4442
If you oppose `window.resize()`, it's worth mentioning that @chriscoyier created a fork of [FitText using a debounced resize method](https://github.com/chriscoyier/FitText.js).
4543

46-
### Fallbacks
47-
As always, use JavaScript with caution: plan for no-js fallbacks that you are comfortable with.
48-
49-
### For Forks' Sake
44+
### Download, Fork, Commit.
5045
If you think you can make this better, please Download, Fork, & Commit. We'd love your see your ideas.

example.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
color:#fff;
2626
font: 95px/1 "Impact";
2727
text-transform: uppercase;
28-
display: inline-block;
28+
display: block;
2929
text-shadow:#253e45 -1px 1px 0,
3030
#253e45 -2px 2px 0,
3131
#d45848 -3px 3px 0,
3232
#d45848 -4px 4px 0;
33-
width: 100%;
3433
margin: 5% auto 5%;
3534
}
3635
</style>
@@ -42,15 +41,19 @@
4241

4342
<div class="container">
4443
<header>
45-
<h1 id="fittext">Squeeze with FitText</h1>
44+
<h1 id="fittext1">Squeeze with FitText</h1>
45+
<h1 id="fittext2">Squeeze with FitText</h1>
46+
<h1 id="fittext3">Squeeze with FitText</h1>
4647
</header>
4748
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
4849
</div>
4950

5051
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
5152
<script src="jquery.fittext.js"></script>
5253
<script type="text/javascript">
53-
$("#fittext").fitText(1.2);
54+
$("#fittext1").fitText();
55+
$("#fittext2").fitText(1.2);
56+
$("#fittext3").fitText(1.1, { minFontSize: 50, maxFontSize: '75px' });
5457
</script>
5558

5659
</body>

jquery.fittext.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,24 @@
1111

1212
(function( $ ){
1313

14-
$.fn.fitText = function( kompressor ) {
14+
$.fn.fitText = function( kompressor, options ) {
15+
16+
var settings = {
17+
'minFontSize' : Number.NEGATIVE_INFINITY,
18+
'maxFontSize' : Number.POSITIVE_INFINITY
19+
};
1520

1621
return this.each(function(){
17-
var $this = $(this); // store the object
18-
var origFontSize = parseFloat($this.css('font-size')); // init the font sizes
19-
var compressor = kompressor || 1; // set the compressor
20-
22+
var $this = $(this); // store the object
23+
var compressor = kompressor || 1; // set the compressor
24+
25+
if ( options ) {
26+
$.extend( settings, options );
27+
}
28+
2129
// Resizer() resizes items based on the object width divided by the compressor * 10
2230
var resizer = function () {
23-
$this.css('font-size', Math.min($this.width() / (compressor*10), origFontSize));
31+
$this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
2432
};
2533

2634
// Call once to set.

0 commit comments

Comments
 (0)