Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to dynamically scale text size based on browser width?

Here is the project this is for: http://phlak.github.com/jColorClock/. As you can see, right now the text size is just set to a static size. I'd like the text to always be ~90% of the width of the window but to also scale the vertical size accordingly. Is there a relatively easy way of doing this?

like image 741
PHLAK Avatar asked Mar 18 '11 21:03

PHLAK


People also ask

How do I automatically adjust font-size in HTML?

Syntax: font-size-adjust: number|none|initial|inherit; Below are the examples that illustrates the use of font-size-adjust property.

How do you scale text in HTML?

In HTML, you can change the size of text with the <font> tag using the size attribute. The size attribute specifies how large a font will be displayed in either relative or absolute terms. Close the <font> tag with </font> to return to a normal text size.

How do I change font-size and width?

The font size can be set with vw (viewport) unit, which means the viewport width. The viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm.

Which CSS property controls the text size in web designing?

The font-size property is used to control the size of fonts. Possible values could be xx-small, x-small, small, medium, large, x-large, xx-large, smaller, larger, size in pixels or in %.


2 Answers

Hell yeah!

Set your <body> font size when the window is resized with a little javascript. (I've used jQuery for convenience here:

$( document ).ready( function() {             var $body = $('body'); //Cache this for performance              var setBodyScale = function() {                 var scaleSource = $body.width(),                     scaleFactor = 0.35,                                          maxScale = 600,                     minScale = 30; //Tweak these values to taste                  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();         }); 

Because your font size is set in em's (perfect) adjusting the percentage font-size of the body element acts as a universal 'text zoom'. This will scale any text set in em's - if you want to be more specific, you could set the percentage font-size on a <div> that surrounds just the elements you want to scale.

Here's a quick example: http://www.spookandpuff.com/examples/dynamicTextSize.html

like image 117
Ben Hull Avatar answered Sep 24 '22 15:09

Ben Hull


New units were added in CSS3 that will allow you to do this. Sitepoint has a good overview. You definitely want to provide a fallback for older browsers, but this is by far the simplest solution:

font-size: 35vmin; 
like image 31
Stephen M. Harris Avatar answered Sep 22 '22 15:09

Stephen M. Harris