Skip to content

Instantly share code, notes, and snippets.

@NeilJS
Created February 12, 2015 09:59
Show Gist options
  • Save NeilJS/6ff52bcd4c9a91435a35 to your computer and use it in GitHub Desktop.
Save NeilJS/6ff52bcd4c9a91435a35 to your computer and use it in GitHub Desktop.
Responsive text - dynamic body font size
<!DOCTYPE html>
<html lang="en" class="js">
<head>
<meta charset="UTF-8">
<title>Responsive text sizes</title>
<style>
h1 {font-size: 1.6em;}
p {font-size: 1em;}
@media (max-width: 768px) {
body {
font-size: 16px !important; /* Keep default font-size for mobiles */
}
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
/* RESIZE TEXT WITH WINDOW SIZE */
var min_font_size = 12; // MIN
var max_font_size = 50; // MAX
var font_resize_rate = 20; // TODO: EXPERIMENT // default 50 // lower more sensitive?
function resize_text() {
var fontSize = $(window).width()/font_resize_rate;
console.log(fontSize);
// Apply size limits
if (fontSize < min_font_size) {
fontSize = min_font_size;
}
if (fontSize > max_font_size) {
fontSize = max_font_size;
}
$('body').css('font-size', fontSize);
}
$(window).resize(function() {
resize_text();
});
resize_text();
});
</script>
</head>
<body>
<h1>Heading 1</h1>
<p>Sizzle interdum you son of a bizzle tellus. Ut uhuh ... yih! adipiscing shiz. Donec fo shizzle est. Things sapizzle crunk, cool nizzle, accumsizzle boom shackalack, fermentizzle the bizzle, pede. Dope nec libero. Etizzle break yo neck, yall ornare ante. Sheezy fo shizzle. Vestibulum ut go to hizzle check it out nibh commodo commodo. Lorem ipsum dolizzle sit shiznit, consectetizzle cool elizzle. Sizzle izzle fo shizzle. Quisque mi sem, sodales owned, nizzle a, eleifend shizzle my nizzle crocodizzle, elit.</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment