auto resize text (font size) when resizing window?

85,435

Solution 1

I've had to do this myself. What I did was that I set a base text size for the body, and percentages for all other sizes. I then used a simple jQuery script to change the base size on window resize. The other sizes then update as well.

I used something like this:

$(function() {
    $(window).bind('resize', function()
    {
        resizeMe();
        }).trigger('resize');
    });

and in the resizeMe-function, I had this:

//Standard height, for which the body font size is correct
var preferredHeight = 768;
//Base font size for the page
var fontsize = 18;

var displayHeight = $(window).height();
var percentage = displayHeight / preferredHeight;
var newFontSize = Math.floor(fontsize * percentage) - 1;
$("body").css("font-size", newFontSize);

Solution 2

Try a jQuery plugin like FitText. It automatically sizes text to fit the width of the parent element.

Another jQuery plugin with the same goal is BigText (demo).

Solution 3

You can do this with CSS3 media queries, specifying different base font-sizes depending on browser size. There is a good article for this on A List Apart

For IE support you might be able to hack it using CSS expressions

This requires you to use a fixed base font-size, for instance on the body tag, and percentage or em based sizes elsewhere.

Solution 4

Building on Lizzan's answer, here is a version using the square root of both width and height to get a proportional sizing:

// When document has finished loading
$(document).ready(function() {

    var resizeText = function () {
        // Standard height, for which the body font size is correct
        var preferredFontSize = 180; // %
        var preferredSize = 1024 * 768;

        var currentSize = $(window).width() * $(window).height();
        var scalePercentage = Math.sqrt(currentSize) / Math.sqrt(preferredSize);
        var newFontSize = preferredFontSize * scalePercentage;
        $("body").css("font-size", newFontSize + '%');
    };

    $(window).bind('resize', function() {
        resizeText();
    }).trigger('resize');

});

Demo: http://jsfiddle.net/tomsoderlund/26Gad/embedded/result/

Solution 5

roryf is on the right track!

Media Queries is (one of) the answer(s).

The trick here is to use % for font sizes everywhere starting from body. This way the font size get's inherited and when you change it in body, it get's changed everywhere.

Try something like:

body { font-size: 100% }
h1 {font-size: 200% }

@media all and (max-width:600px) {
    body {font-size: 70%}
}

When the website width gets smaller then 600px, then the font size will turn to 70% of what it was everywhere, where % is used (also h1 in this example.)

Share:
85,435
Stephen
Author by

Stephen

Updated on November 11, 2020

Comments

  • Stephen
    Stephen over 3 years

    I have been trying(in vain) to build a page whose elements would resize as I changed the window size. I have it working in css for images no problem, but I can't seem to accomplish the same for text, and I am not sure it is even possible in CSS. And I can't seem to find a jquery script that accomplishes this.

    When a user resizes the window, I essentially want the page to scale dynamically and fluidly, without the user having to invoke page zoom. This works fine on my img divs via css, but not for the text, which stays the same size.

    Any ideas?