Created
January 11, 2012 05:12
-
-
Save ramseyp/1593158 to your computer and use it in GitHub Desktop.
resize text based off container size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Dynamic text sizing</title> | |
<!--[if IE]> | |
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> | |
<![endif]--> | |
<!-- From @Beejamin - http://stackoverflow.com/users/79068/beejamin --> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript" charset="utf-8"></script> | |
<script type="text/javascript" charset="utf-8"> | |
$( document ).ready( function() { | |
var $body = $('body'); //Cache this for performance | |
var setBodyScale = function() { | |
var scaleFactor = 0.35, | |
scaleSource = $body.width(), | |
maxScale = 600, | |
minScale = 30; | |
var fontSize = scaleSource * scaleFactor; //Multiply the width of the body by the scaling factor: | |
if (fontSize > maxScale) fontSize = maxScale; | |
if (fontSize < minScale) fontSize = minScale; //Enforce the minimum and maximums | |
$('body').css('font-size', fontSize + '%'); | |
} | |
$(window).resize(function(){ | |
setBodyScale(); | |
}); | |
//Fire it when the page first loads: | |
setBodyScale(); | |
}); | |
</script> | |
<style type="text/css" media="screen"> | |
h1 {font-size:2em} | |
</style> | |
</head> | |
<body> | |
<h1>Grow and shrink</h1> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment